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

taosdata / TDengine / #4941

27 Jan 2026 10:23AM UTC coverage: 66.868% (+0.04%) from 66.832%
#4941

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

560 existing lines in 126 files now uncovered.

204401 of 305680 relevant lines covered (66.87%)

126915843.15 hits per line

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

69.54
/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) {
638,570,165✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
638,570,165✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
638,570,165✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
638,674,247✔
47
}
638,556,158✔
48

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

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

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

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

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
48,170,396✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
48,170,396✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
48,171,213✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
22,065,245✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
22,065,580✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
22,064,661✔
162
      if (pScanInfo->base.dataReader != NULL) {
22,064,326✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
259,174✔
164
        if (code) {
258,297✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
26,104,495✔
172
  }
173

174
  return 0;
25,581,454✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
25,581,815✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
25,581,815✔
179
  pTaskInfo->id.queryId = queryId;
25,581,815✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
25,582,331✔
181

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

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

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

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

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

248
  return pTaskInfo;
320,262✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
542,649✔
252
  if (streamInserterParam == NULL) {
542,649✔
253
    return TSDB_CODE_SUCCESS;
319,961✔
254
  }
255

256
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
222,688✔
257
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
258
    return TSDB_CODE_INVALID_PARA;
×
259
  }
260

261
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
222,688✔
262
    stError("insertParam: invalid db/table name");
×
263
    return TSDB_CODE_INVALID_PARA;
×
264
  }
265

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

272
  return TSDB_CODE_SUCCESS;
222,688✔
273
}
274

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

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

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

310
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
222,688✔
311
    if (NULL == pInserterParam) {
222,688✔
312
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
313
      code = terrno;
×
314
      goto _error;
×
315
    }
316
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
222,688✔
317
    TSDB_CHECK_CODE(code, lino, _error);
222,688✔
318
    
319
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
222,688✔
320
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
222,688✔
321

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
222,688✔
323
    if (code) {
222,688✔
324
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
325
    }
326
  }
327
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
542,649✔
328
         tstrerror(code));
329

330
_error:
72,886✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,974,102✔
342
  if (pInfo == NULL) {
2,974,102✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,974,102✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,974,102✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,974,102✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,974,102✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,662,387✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
479,239✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,636,489✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,974,102✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
311,715✔
382
    setReadHandle(handle, pScanBaseInfo);
311,715✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,662,387✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,183,148✔
385
    setReadHandle(handle, pScanBaseInfo);
2,183,148✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
542,649✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
376,390✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
376,390✔
444

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

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

454
  locked = 1;
376,390✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
829,964✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
453,574✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
453,574✔
458

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

465
    tDecoderClear(&mr.coder);
453,574✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
453,574✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
453,574✔
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) {
453,574✔
473
          continue;
×
474
        }
475
      } else {  // ordinary table
476
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
477
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
478
        if (mr.me.uid != uid) {
×
479
          continue;
×
480
        }
481
      }
482
    }
483

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
376,390✔
524

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

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

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

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

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
377,025✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
377,025✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
377,025✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
377,025✔
558
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
×
559
      int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
×
560

561
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
562
    }
563
  }
564

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

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

588
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
376,390✔
589
    taosWLockLatch(&pTaskInfo->lock);
376,390✔
590

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

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

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

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

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

638
  return code;
377,025✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
499,233,908✔
647
    return TSDB_CODE_INVALID_PARA;
×
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
499,289,968✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
499,289,968✔
652
    return TSDB_CODE_SUCCESS;
293,441,470✔
653
  }
654

655
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
205,807,035✔
656
  if (!pSchemaInfo) {
205,677,053✔
657
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2,678✔
658
    return terrno;
2,678✔
659
  }
660

661
  *sversion = pSchemaInfo->sw->version;
205,674,392✔
662
  *tversion = pSchemaInfo->tversion;
205,877,669✔
663
  *rversion = pSchemaInfo->rversion;
205,845,527✔
664
  if (pSchemaInfo->dbname) {
205,969,473✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
205,923,391✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
206,054,485✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
205,997,555✔
671
  } else {
672
    tableName[0] = 0;
25,098✔
673
  }
674

675
  *tbGet = true;
206,051,841✔
676

677
  return TSDB_CODE_SUCCESS;
205,999,399✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
293,267,357✔
681

682
void qDestroyOperatorParam(SOperatorParam* pParam) {
122,428✔
683
  if (NULL == pParam) {
122,428✔
684
    return;
×
685
  }
686
  freeOperatorParam(pParam, OP_GET_PARAM);
122,428✔
687
}
688

689
/**
690
  @brief Update the operator param for the task.
691
  @note  Unlike setOperatorParam, this function will destroy the new param when
692
         operator type mismatch.
693
*/
694
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
32,478,328✔
695
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
32,478,328✔
696
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
32,478,328✔
697
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
32,478,328✔
698
    qError("%s, %s operator type mismatch, task operator type:%d, "
118,572✔
699
           "new param operator type:%d", GET_TASKID(pTask), __func__,
700
           pTask->pRoot->operatorType,
701
           pNewParam->opType);
702
    qDestroyOperatorParam((SOperatorParam*)pParam);
118,572✔
703
    return;
118,572✔
704
  }
705
  TSWAP(pParam, pTask->pOpParam);
32,362,607✔
706
  ((SExecTaskInfo*)tinfo)->paramSet = false;
32,356,685✔
707
}
708

709
int32_t qExecutorInit(void) {
4,202,894✔
710
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,202,894✔
711
  return TSDB_CODE_SUCCESS;
4,203,675✔
712
}
713

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
20,546,863✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
20,546,863✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
20,546,863✔
717
  if (pTask->pWorkerCb) {
20,546,863✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
20,553,379✔
719
    if (code != TSDB_CODE_SUCCESS) {
20,569,594✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724

725
  code = tsem_wait(pSem);
20,564,104✔
726
  if (code != TSDB_CODE_SUCCESS) {
20,568,026✔
727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
66✔
728
    pTask->code = code;
×
729
    return pTask->code;
×
730
  }
731

732
  if (pTask->pWorkerCb) {
20,567,974✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
20,568,487✔
734
    if (code != TSDB_CODE_SUCCESS) {
20,570,574✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
20,570,061✔
740
}
741

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

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

751
  readHandle->uid = 0;
298,908,722✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
298,954,417✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
298,501,262✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
4,090,395✔
755
    goto _error;
3,503,793✔
756
  }
757
    
758
  if (handle) {
294,505,972✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
294,603,520✔
760
    void*           pSinkManager = NULL;
294,730,184✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
294,005,899✔
762
    if (code != TSDB_CODE_SUCCESS) {
294,605,394✔
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
294,605,394✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
294,613,751✔
769
    if (code != TSDB_CODE_SUCCESS) {
293,975,494✔
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
771
      taosMemoryFree(pSinkManager);
×
772
      goto _error;
×
773
    }
774

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

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
294,253,254✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
294,715,296✔
789
    if (code) {
294,034,286✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
579✔
791
    }
792
  }
793

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

797
_error:
93,304,227✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
298,769,403✔
800
}
801

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

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

814
  if (pLocal) {
342,648,893✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
339,273,693✔
816
  }
817

818
  taosArrayClear(pResList);
342,511,527✔
819

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

827
  if (pTaskInfo->cost.start == 0) {
342,612,680✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
289,456,561✔
829
  }
830

831
  if (isTaskKilled(pTaskInfo)) {
342,732,210✔
832
    atomic_store_64(&pTaskInfo->owner, 0);
80✔
833
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
80✔
834
    return pTaskInfo->code;
80✔
835
  }
836

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
342,586,815✔
839
  if (ret != TSDB_CODE_SUCCESS) {
345,834,865✔
840
    pTaskInfo->code = ret;
3,483,246✔
841
    (void)cleanUpUdfs();
3,483,246✔
842

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

846
    return pTaskInfo->code;
3,483,246✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
342,351,619✔
850

851
  int32_t      current = 0;
342,353,474✔
852
  SSDataBlock* pRes = NULL;
342,353,474✔
853
  int64_t      st = taosGetTimestampUs();
342,688,343✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
342,688,343✔
856
    pTaskInfo->paramSet = true;
32,355,272✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
32,354,553✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
310,287,462✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
339,300,012✔
863
  code = blockDataCheck(pRes);
339,300,012✔
864
  QUERY_CHECK_CODE(code, lino, _end);
339,304,208✔
865

866
  if (pRes == NULL) {
339,304,208✔
867
    st = taosGetTimestampUs();
65,768,542✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
339,327,748✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
339,322,586✔
872
    rowsThreshold = 4096;
338,774,032✔
873
  }
874

875
  int32_t blockIndex = 0;
339,314,457✔
876
  while (pRes != NULL) {
856,395,871✔
877
    SSDataBlock* p = NULL;
562,099,220✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
562,063,523✔
879
      SSDataBlock* p1 = NULL;
440,896,384✔
880
      code = createOneDataBlock(pRes, true, &p1);
440,899,935✔
881
      QUERY_CHECK_CODE(code, lino, _end);
440,952,676✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
440,952,676✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
440,970,330✔
885
      p = p1;
440,970,330✔
886
    } else {
887
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
121,178,580✔
888
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
121,179,778✔
889

890
      p = *(SSDataBlock**)tmp;
121,179,778✔
891
      code = copyDataBlock(p, pRes);
121,179,778✔
892
      QUERY_CHECK_CODE(code, lino, _end);
121,177,913✔
893
    }
894

895
    blockIndex += 1;
562,143,687✔
896

897
    current += p->info.rows;
562,143,687✔
898
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
562,151,491✔
899
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
900
    void* tmp = taosArrayPush(pResList, &p);
562,122,075✔
901
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
562,122,075✔
902

903
    if (current >= rowsThreshold || processOneBlock) {
562,122,075✔
904
      break;
905
    }
906

907
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
517,056,719✔
908
    QUERY_CHECK_CODE(code, lino, _end);
517,040,885✔
909
    code = blockDataCheck(pRes);
517,040,885✔
910
    QUERY_CHECK_CODE(code, lino, _end);
517,051,620✔
911
  }
912

913
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
339,362,577✔
914
    pTaskInfo->pSubplan->rowsThreshold -= current;
507,038✔
915
  }
916

917
  *hasMore = (pRes != NULL);
339,355,176✔
918
  uint64_t el = (taosGetTimestampUs() - st);
339,309,582✔
919

920
  pTaskInfo->cost.elapsedTime += el;
339,309,582✔
921
  if (NULL == pRes) {
339,332,094✔
922
    *useconds = pTaskInfo->cost.elapsedTime;
294,247,555✔
923
  }
924

925
_end:
300,785,769✔
926
  (void)cleanUpUdfs();
339,339,781✔
927

928
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
339,446,676✔
929
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
339,446,676✔
930
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
931

932
  atomic_store_64(&pTaskInfo->owner, 0);
339,446,314✔
933
  if (code) {
339,445,778✔
934
    pTaskInfo->code = code;
×
935
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
936
  }
937

938
  return pTaskInfo->code;
339,445,778✔
939
}
940

941
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
942
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
943
  SArray*        pList = pTaskInfo->pResultBlockList;
×
944
  size_t         num = taosArrayGetSize(pList);
×
945
  for (int32_t i = 0; i < num; ++i) {
×
946
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
947
    if (p) {
×
948
      blockDataDestroy(*p);
×
949
    }
950
  }
951

952
  taosArrayClear(pTaskInfo->pResultBlockList);
×
953
}
×
954

955
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
117,759,646✔
956
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
117,759,646✔
957
  int64_t        threadId = taosGetSelfPthreadId();
117,759,646✔
958
  int64_t        curOwner = 0;
117,759,981✔
959

960
  *pRes = NULL;
117,759,981✔
961

962
  // todo extract method
963
  taosRLockLatch(&pTaskInfo->lock);
117,759,981✔
964
  bool isKilled = isTaskKilled(pTaskInfo);
117,759,981✔
965
  if (isKilled) {
117,759,360✔
966
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
967

968
    taosRUnLockLatch(&pTaskInfo->lock);
×
969
    return pTaskInfo->code;
×
970
  }
971

972
  if (pTaskInfo->owner != 0) {
117,759,360✔
973
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
974
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
975

976
    taosRUnLockLatch(&pTaskInfo->lock);
×
977
    return pTaskInfo->code;
×
978
  }
979

980
  pTaskInfo->owner = threadId;
117,758,976✔
981
  taosRUnLockLatch(&pTaskInfo->lock);
117,758,976✔
982

983
  if (pTaskInfo->cost.start == 0) {
117,758,980✔
984
    pTaskInfo->cost.start = taosGetTimestampUs();
225,149✔
985
  }
986

987
  // error occurs, record the error code and return to client
988
  int32_t ret = setjmp(pTaskInfo->env);
117,759,985✔
989
  if (ret != TSDB_CODE_SUCCESS) {
117,756,398✔
990
    pTaskInfo->code = ret;
×
991
    (void)cleanUpUdfs();
×
992
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
993
    atomic_store_64(&pTaskInfo->owner, 0);
×
994
    return pTaskInfo->code;
×
995
  }
996

997
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
117,756,398✔
998

999
  int64_t st = taosGetTimestampUs();
117,758,689✔
1000
  int32_t code = TSDB_CODE_SUCCESS;
117,758,689✔
1001
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
117,758,689✔
1002
    pTaskInfo->paramSet = true;
×
1003
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1004
  } else {
1005
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
117,758,354✔
1006
  }
1007
  if (code) {
117,723,174✔
1008
    pTaskInfo->code = code;
×
1009
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1010
  }
1011

1012
  code = blockDataCheck(*pRes);
117,723,174✔
1013
  if (code) {
117,758,102✔
1014
    pTaskInfo->code = code;
×
1015
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1016
  }
1017

1018
  uint64_t el = (taosGetTimestampUs() - st);
117,728,370✔
1019

1020
  pTaskInfo->cost.elapsedTime += el;
117,728,370✔
1021
  if (NULL == *pRes) {
117,747,719✔
1022
    *useconds = pTaskInfo->cost.elapsedTime;
21,668,211✔
1023
  }
1024

1025
  (void)cleanUpUdfs();
117,750,494✔
1026

1027
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
117,760,320✔
1028
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
117,760,320✔
1029

1030
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
117,760,320✔
1031
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1032

1033
  atomic_store_64(&pTaskInfo->owner, 0);
117,758,936✔
1034
  return pTaskInfo->code;
117,759,985✔
1035
}
1036

1037
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
102,667,097✔
1038
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
102,667,097✔
1039
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
102,671,192✔
1040
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
102,671,039✔
1041

1042
  if (!tmp) {
102,666,192✔
1043
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
546✔
1044
    return terrno;
546✔
1045
  }
1046
  return TSDB_CODE_SUCCESS;
102,665,646✔
1047
}
1048

1049
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1050
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1051
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1052

1053
  if (key->refId < pInfo->refId) {
×
1054
    return -1;
×
1055
  } else if (key->refId > pInfo->refId) {
×
1056
    return 1;
×
1057
  }
1058

1059
  return 0;
×
1060
}
1061

1062
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1063
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1064
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1065
  if (idx >= 0) {
×
1066
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1067
  }
1068
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1069
}
×
1070

1071
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
164,191✔
1072
  if (pTaskInfo->subJobCtx.hasSubJobs) {
164,191✔
1073
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
100,940✔
1074
    if (pTaskInfo->subJobCtx.param) {
100,940✔
1075
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
56,786✔
1076
    }
1077
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
100,940✔
1078
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
100,940✔
1079
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
100,940✔
1080
  }
1081
  
1082
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
164,191✔
1083

1084
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
164,191✔
1085
  for (int32_t i = 0; i < num; ++i) {
176,953✔
1086
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
12,762✔
1087
    if (!pStop) {
12,762✔
1088
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1089
      continue;
×
1090
    }
1091
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
12,762✔
1092
    if (pExchangeInfo) {
12,762✔
1093
      int32_t code = tsem_post(&pExchangeInfo->ready);
12,762✔
1094
      if (code != TSDB_CODE_SUCCESS) {
12,762✔
1095
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1096
      } else {
1097
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
12,762✔
1098
      }
1099
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
12,762✔
1100
      if (code != TSDB_CODE_SUCCESS) {
12,762✔
1101
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1102
      }
1103
    }
1104
  }
1105

1106
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
164,191✔
1107
}
164,191✔
1108

1109
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
164,191✔
1110
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
164,191✔
1111
  if (pTaskInfo == NULL) {
164,191✔
1112
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1113
  }
1114

1115
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
164,191✔
1116

1117
  setTaskKilled(pTaskInfo, rspCode);
164,191✔
1118
  qStopTaskOperators(pTaskInfo);
164,191✔
1119

1120
  return TSDB_CODE_SUCCESS;
164,191✔
1121
}
1122

1123
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1124
  int64_t        st = taosGetTimestampMs();
×
1125
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1126
  if (pTaskInfo == NULL) {
×
1127
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1128
  }
1129

1130
  if (waitDuration > 0) {
×
1131
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1132
  } else {
1133
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1134
  }
1135

1136
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1137

1138
  if (waitDuration > 0) {
×
1139
    while (1) {
1140
      taosWLockLatch(&pTaskInfo->lock);
×
1141
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1142
        taosWUnLockLatch(&pTaskInfo->lock);
×
1143

1144
        taosMsleep(200);
×
1145

1146
        int64_t d = taosGetTimestampMs() - st;
×
1147
        if (d >= waitDuration && waitDuration >= 0) {
×
1148
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1149
          return TSDB_CODE_SUCCESS;
×
1150
        }
1151
      } else {  // not running now
1152
        pTaskInfo->code = rspCode;
×
1153
        taosWUnLockLatch(&pTaskInfo->lock);
×
1154
        return TSDB_CODE_SUCCESS;
×
1155
      }
1156
    }
1157
  }
1158

1159
  int64_t et = taosGetTimestampMs() - st;
×
1160
  if (et < waitDuration) {
×
1161
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1162
    return TSDB_CODE_SUCCESS;
×
1163
  }
1164
  return TSDB_CODE_SUCCESS;
×
1165
}
1166

1167
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1168
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1169
  if (NULL == pTaskInfo) {
×
1170
    return false;
×
1171
  }
1172

1173
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1174
}
1175

1176
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
296,097,841✔
1177
  STaskCostInfo* pSummary = &pTaskInfo->cost;
296,097,841✔
1178
  int64_t        idleTime = pSummary->start - pSummary->created;
296,104,400✔
1179

1180
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
296,074,545✔
1181
  if (pSummary->pRecoder != NULL) {
296,052,532✔
1182
    qDebug(
203,734,841✔
1183
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1184
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1185
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1186
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1187
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1188
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1189
  } else {
1190
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
92,344,241✔
1191
           pSummary->elapsedTime / 1000.0);
1192
  }
1193
}
296,079,082✔
1194

1195

1196
void qDestroyTask(qTaskInfo_t qTaskHandle) {
305,432,467✔
1197
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
305,432,467✔
1198
  if (pTaskInfo == NULL) {
305,432,467✔
1199
    return;
9,343,575✔
1200
  }
1201

1202
  if (pTaskInfo->pRoot != NULL) {
296,088,892✔
1203
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
296,111,185✔
1204
  } else {
1205
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1206
  }
1207

1208
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
296,086,625✔
1209
  doDestroyTask(pTaskInfo);
296,107,500✔
1210
}
1211

1212
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
12,431,137✔
1213
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
12,431,137✔
1214
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
12,431,137✔
1215
}
1216

1217
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
319,994✔
1218
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
319,994✔
1219
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
319,994✔
1220

1221
  while (1) {
314,300✔
1222
    uint16_t type = pOperator->operatorType;
634,562✔
1223
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
634,562✔
1224
      *scanner = pOperator->info;
320,262✔
1225
      break;
320,262✔
1226
    } else {
1227
      pOperator = pOperator->pDownstream[0];
314,300✔
1228
    }
1229
  }
1230
}
320,262✔
1231

1232
void* qExtractReaderFromTmqScanner(void* scanner) {
320,262✔
1233
  SStreamScanInfo* pInfo = scanner;
320,262✔
1234
  return (void*)pInfo->tqReader;
320,262✔
1235
}
1236

1237
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
708,355✔
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
708,355✔
1239
  return pTaskInfo->streamInfo.schema;
708,355✔
1240
}
1241

1242
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
708,355✔
1243
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
708,355✔
1244
  return pTaskInfo->streamInfo.tbName;
708,355✔
1245
}
1246

1247
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
786,630✔
1248
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
786,630✔
1249
  return &pTaskInfo->streamInfo.btMetaRsp;
786,630✔
1250
}
1251

1252
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
22,700,895✔
1253
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,700,895✔
1254
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
22,700,895✔
1255
  return 0;
22,700,895✔
1256
  /*if (code != TSDB_CODE_SUCCESS) {
1257
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1258
    pTaskInfo->code = code;
1259
    T_LONG_JMP(pTaskInfo->env, code);
1260
  }*/
1261
}
1262

1263
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
750,117✔
1264
  memset(pCond, 0, sizeof(SQueryTableDataCond));
750,117✔
1265
  pCond->order = TSDB_ORDER_ASC;
750,117✔
1266
  pCond->numOfCols = pMtInfo->schema->nCols;
751,242✔
1267
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
751,863✔
1268
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
751,574✔
1269
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
751,865✔
UNCOV
1270
    taosMemoryFreeClear(pCond->colList);
×
1271
    taosMemoryFreeClear(pCond->pSlotList);
×
1272
    return terrno;
×
1273
  }
1274

1275
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
752,156✔
1276
  pCond->suid = pMtInfo->suid;
751,574✔
1277
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
750,701✔
1278
  pCond->startVersion = -1;
750,656✔
1279
  pCond->endVersion = sContext->snapVersion;
752,156✔
1280

1281
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,785,666✔
1282
    SColumnInfo* pColInfo = &pCond->colList[i];
4,032,055✔
1283
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,032,633✔
1284
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,032,633✔
1285
    if (pMtInfo->pExtSchemas != NULL) {
4,033,174✔
1286
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,400✔
1287
    }
1288
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,032,631✔
1289
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,030,889✔
1290

1291
    pCond->pSlotList[i] = i;
4,034,053✔
1292
  }
1293

1294
  return TSDB_CODE_SUCCESS;
751,865✔
1295
}
1296

1297
void qStreamSetOpen(qTaskInfo_t tinfo) {
116,278,137✔
1298
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
116,278,137✔
1299
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
116,278,137✔
1300
  pOperator->status = OP_NOT_OPENED;
116,286,427✔
1301
}
116,283,268✔
1302

1303
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
21,894,450✔
1304
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,894,450✔
1305
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
21,894,450✔
1306
}
21,894,995✔
1307

1308
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
22,865,024✔
1309
  int32_t        code = TSDB_CODE_SUCCESS;
22,865,024✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,865,024✔
1311
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
22,865,024✔
1312

1313
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,866,259✔
1314
  const char*    id = GET_TASKID(pTaskInfo);
22,865,291✔
1315

1316
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,864,147✔
1317
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
21,601,953✔
1318
    if (pOperator == NULL || code != 0) {
21,598,836✔
1319
      return code;
15✔
1320
    }
1321

1322
    SStreamScanInfo* pInfo = pOperator->info;
21,599,127✔
1323
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
21,598,404✔
1324
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
21,601,686✔
1325
    walReaderVerifyOffset(pWalReader, pOffset);
21,600,633✔
1326
  }
1327
  // if pOffset equal to current offset, means continue consume
1328
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,863,534✔
1329
    return 0;
20,987,897✔
1330
  }
1331

1332
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,875,126✔
1333
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,105,715✔
1334
    if (pOperator == NULL || code != 0) {
1,105,431✔
1335
      return code;
298✔
1336
    }
1337

1338
    SStreamScanInfo* pInfo = pOperator->info;
1,105,141✔
1339
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,105,131✔
1340
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,105,751✔
1341
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,104,813✔
1342

1343
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,105,731✔
1344
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
902,203✔
1345
      pScanBaseInfo->dataReader = NULL;
901,308✔
1346

1347
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
901,308✔
1348
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
901,308✔
1349
      walReaderVerifyOffset(pWalReader, pOffset);
901,868✔
1350
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
902,203✔
1351
      if (code < 0) {
902,203✔
1352
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
12,122✔
1353
        return code;
12,122✔
1354
      }
1355
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
202,322✔
1356
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1357
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1358
      int64_t uid = pOffset->uid;
203,834✔
1359
      int64_t ts = pOffset->ts;
202,940✔
1360
      int32_t index = 0;
203,228✔
1361

1362
      // this value may be changed if new tables are created
1363
      taosRLockLatch(&pTaskInfo->lock);
203,228✔
1364
      int32_t numOfTables = 0;
203,834✔
1365
      code = tableListGetSize(pTableListInfo, &numOfTables);
203,834✔
1366
      if (code != TSDB_CODE_SUCCESS) {
203,834✔
1367
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1368
        taosRUnLockLatch(&pTaskInfo->lock);
×
1369
        return code;
×
1370
      }
1371

1372
      if (uid == 0) {
203,834✔
1373
        if (numOfTables != 0) {
195,696✔
1374
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,588✔
1375
          if (!tmp) {
38,588✔
1376
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1377
            taosRUnLockLatch(&pTaskInfo->lock);
×
1378
            return terrno;
×
1379
          }
1380
          if (tmp) uid = tmp->uid;
38,588✔
1381
          ts = INT64_MIN;
38,588✔
1382
          pScanInfo->currentTable = 0;
38,588✔
1383
        } else {
1384
          taosRUnLockLatch(&pTaskInfo->lock);
157,108✔
1385
          qError("no table in table list, %s", id);
157,108✔
1386
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
157,108✔
1387
        }
1388
      }
1389
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
46,726✔
1390

1391
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
46,726✔
1392
             pInfo->pTableScanOp->resultInfo.totalRows);
1393
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
46,726✔
1394

1395
      // start from current accessed position
1396
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1397
      // position, let's find it from the beginning.
1398
      index = tableListFind(pTableListInfo, uid, 0);
46,726✔
1399
      taosRUnLockLatch(&pTaskInfo->lock);
46,726✔
1400

1401
      if (index >= 0) {
46,726✔
1402
        pScanInfo->currentTable = index;
46,726✔
1403
      } else {
1404
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1405
               numOfTables, pScanInfo->currentTable, id);
1406
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1407
      }
1408

1409
      STableKeyInfo keyInfo = {.uid = uid};
46,726✔
1410
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
46,726✔
1411

1412
      // let's start from the next ts that returned to consumer.
1413
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
46,726✔
1414
        pScanBaseInfo->cond.twindows.skey = ts;
927✔
1415
      } else {
1416
        pScanBaseInfo->cond.twindows.skey = ts + 1;
45,799✔
1417
      }
1418
      pScanInfo->scanTimes = 0;
46,726✔
1419

1420
      if (pScanBaseInfo->dataReader == NULL) {
46,726✔
1421
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
80,728✔
1422
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1423
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
40,364✔
1424
        if (code != TSDB_CODE_SUCCESS) {
40,364✔
1425
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1426
          return code;
×
1427
        }
1428

1429
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
40,364✔
1430
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1431
      } else {
1432
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,362✔
1433
        if (code != TSDB_CODE_SUCCESS) {
6,362✔
1434
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1435
          return code;
×
1436
        }
1437

1438
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,362✔
1439
        if (code != TSDB_CODE_SUCCESS) {
6,362✔
1440
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1441
          return code;
×
1442
        }
1443
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
6,362✔
1444
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1445
      }
1446

1447
      // restore the key value
1448
      pScanBaseInfo->cond.twindows.skey = oldSkey;
46,726✔
1449
    } else {
1450
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1451
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1452
    }
1453

1454
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1455
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
769,411✔
1456
      SStreamRawScanInfo* pInfo = pOperator->info;
754,167✔
1457
      SSnapContext*       sContext = pInfo->sContext;
754,167✔
1458
      SOperatorInfo*      p = NULL;
754,167✔
1459

1460
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
754,167✔
1461
      if (code != 0) {
754,167✔
1462
        return code;
×
1463
      }
1464

1465
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
754,167✔
1466

1467
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
754,167✔
1468
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1469
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1470
      }
1471

1472
      SMetaTableInfo mtInfo = {0};
754,167✔
1473
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
754,167✔
1474
      if (code != 0) {
753,294✔
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
753,294✔
1479
      pInfo->dataReader = NULL;
752,128✔
1480

1481
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
752,419✔
1482
      tableListClear(pTableListInfo);
753,585✔
1483

1484
      if (mtInfo.uid == 0) {
754,167✔
1485
        destroyMetaTableInfo(&mtInfo);
1486
        goto end;  // no data
847✔
1487
      }
1488

1489
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
753,320✔
1490
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
750,699✔
1491
      if (code != TSDB_CODE_SUCCESS) {
750,117✔
1492
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1493
        destroyMetaTableInfo(&mtInfo);
1494
        return code;
×
1495
      }
1496
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
750,117✔
1497
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,291✔
1498
      } else {
1499
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
749,117✔
1500
      }
1501

1502
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
750,699✔
1503
      if (code != TSDB_CODE_SUCCESS) {
753,320✔
1504
        destroyMetaTableInfo(&mtInfo);
1505
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1506
        return code;
×
1507
      }
1508

1509
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
753,320✔
1510
      if (!pList) {
753,320✔
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
1513
        return code;
×
1514
      }
1515
      int32_t size = 0;
753,320✔
1516
      code = tableListGetSize(pTableListInfo, &size);
753,320✔
1517
      if (code != TSDB_CODE_SUCCESS) {
753,320✔
1518
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1519
        destroyMetaTableInfo(&mtInfo);
1520
        return code;
×
1521
      }
1522

1523
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,506,640✔
1524
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
753,320✔
1525
      if (code != TSDB_CODE_SUCCESS) {
751,574✔
1526
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1527
        destroyMetaTableInfo(&mtInfo);
1528
        return code;
×
1529
      }
1530

1531
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
751,574✔
1532
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
751,572✔
1533
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1534
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
750,699✔
1535
      pTaskInfo->streamInfo.schema = mtInfo.schema;
750,697✔
1536
      taosMemoryFreeClear(mtInfo.pExtSchemas);
753,027✔
1537

1538
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
753,027✔
1539
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
15,244✔
1540
      SStreamRawScanInfo* pInfo = pOperator->info;
4,805✔
1541
      SSnapContext*       sContext = pInfo->sContext;
4,805✔
1542
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,805✔
1543
      if (code != 0) {
4,805✔
1544
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1545
        return code;
×
1546
      }
1547
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,805✔
1548
             id);
1549
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
10,439✔
1550
      SStreamRawScanInfo* pInfo = pOperator->info;
10,439✔
1551
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
10,439✔
1552
      pInfo->dataReader = NULL;
10,439✔
1553
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
10,439✔
1554
    }
1555
  }
1556

1557
end:
1,706,112✔
1558
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,706,218✔
1559
  return 0;
1,706,218✔
1560
}
1561

1562
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
237,855,803✔
1563
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
237,855,803✔
1564
  if (pMsg->info.ahandle == NULL) {
237,875,083✔
1565
    rpcFreeCont(pMsg->pCont);
×
1566
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
×
1567
    return;
×
1568
  }
1569

1570
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
237,830,128✔
1571
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1572

1573
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
237,844,492✔
1574

1575
  if (pMsg->contLen > 0) {
237,867,245✔
1576
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
237,834,934✔
1577
    if (buf.pData == NULL) {
237,791,100✔
1578
      pMsg->code = terrno;
×
1579
    } else {
1580
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
237,791,100✔
1581
    }
1582
  }
1583

1584
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
237,901,845✔
1585
  rpcFreeCont(pMsg->pCont);
237,940,004✔
1586
  destroySendMsgInfo(pSendInfo);
237,863,917✔
1587
}
1588

1589
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1590
  int32_t        code = TSDB_CODE_SUCCESS;
×
1591
  int32_t        lino = 0;
×
1592
  SExecTaskInfo* pTaskInfo = tinfo;
×
1593
  SArray*        plist = NULL;
×
1594

1595
  code = getTableListInfo(pTaskInfo, &plist);
×
1596
  if (code || plist == NULL) {
×
1597
    return NULL;
×
1598
  }
1599

1600
  // only extract table in the first elements
1601
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1602

1603
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1604
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1605

1606
  int32_t numOfTables = 0;
×
1607
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1608
  QUERY_CHECK_CODE(code, lino, _end);
×
1609

1610
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1611
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1612
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1613
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1614
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1615
  }
1616

1617
  taosArrayDestroy(plist);
×
1618

1619
_end:
×
1620
  if (code != TSDB_CODE_SUCCESS) {
×
1621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1622
    T_LONG_JMP(pTaskInfo->env, code);
×
1623
  }
1624
  return pUidList;
×
1625
}
1626

1627
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,335,761✔
1628
  int32_t        code = TSDB_CODE_SUCCESS;
3,335,761✔
1629
  int32_t        lino = 0;
3,335,761✔
1630
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,335,761✔
1631

1632
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,337,366✔
1633
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1634
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1635

1636
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1637
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1638
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,335,758✔
1639
    STableScanInfo* pScanInfo = pOperator->info;
1,669,217✔
1640

1641
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,669,217✔
1642
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,669,217✔
1643
  } else {
1644
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,667,612✔
1645
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,667,612✔
1646
    }
1647
  }
1648

1649
_end:
×
1650
  if (code != TSDB_CODE_SUCCESS) {
3,336,292✔
1651
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1652
  }
1653
  return code;
3,335,758✔
1654
}
1655

1656
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,667,612✔
1657
  if (pList == NULL) {
1,667,612✔
1658
    return TSDB_CODE_INVALID_PARA;
×
1659
  }
1660

1661
  *pList = NULL;
1,667,612✔
1662
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,668,146✔
1663
  if (pArray == NULL) {
1,667,612✔
1664
    return terrno;
×
1665
  }
1666

1667
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,667,612✔
1668
  if (code == 0) {
1,668,146✔
1669
    *pList = pArray;
1,668,146✔
1670
  } else {
1671
    taosArrayDestroy(pArray);
×
1672
  }
1673
  return code;
1,667,075✔
1674
}
1675

1676
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1677
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1678
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1679
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1680
  }
1681
  return 0;
×
1682
}
1683

1684
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1685
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1686
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1687
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1688
  }
1689
  return 0;
×
1690
}
1691

1692
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1693
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1694

1695
  int32_t code = pTaskInfo->code;
×
1696
  pTaskInfo->code = 0;
×
1697
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1698
}
×
1699

1700
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1701
  int32_t code = 0;
×
1702
  return code;
×
1703
}
1704

1705
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1706
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1707
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1708
  return code;
×
1709
}
1710

1711
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
24,815,388✔
1712
  int32_t code = 0;
24,815,388✔
1713

1714
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
24,815,388✔
1715
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
24,815,263✔
1716

1717
  if (pOper->fpSet.resetStateFn) {
24,815,236✔
1718
    code = pOper->fpSet.resetStateFn(pOper);
24,815,652✔
1719
  }
1720
  pOper->status = OP_NOT_OPENED;
24,812,922✔
1721
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
42,379,186✔
1722
    code = clearStatesForOperator(pOper->pDownstream[i]);
17,562,650✔
1723
  }
1724
  return code;
24,818,044✔
1725
}
1726

1727
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,253,031✔
1728
  int32_t        code = 0;
7,253,031✔
1729
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,253,031✔
1730
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,253,031✔
1731
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,252,817✔
1732
  code = clearStatesForOperator(pOper);
7,252,823✔
1733
  return code;
7,252,605✔
1734
}
1735

1736
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
9,941,999✔
1737
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
9,941,999✔
1738
  int64_t        threadId = taosGetSelfPthreadId();
9,941,999✔
1739
  int64_t        curOwner = 0;
9,942,212✔
1740

1741
  *ppRes = NULL;
9,942,212✔
1742

1743
  // todo extract method
1744
  taosRLockLatch(&pTaskInfo->lock);
9,942,425✔
1745
  bool isKilled = isTaskKilled(pTaskInfo);
9,942,425✔
1746
  if (isKilled) {
9,942,425✔
1747
    // clearStreamBlock(pTaskInfo->pRoot);
1748
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1749

1750
    taosRUnLockLatch(&pTaskInfo->lock);
×
1751
    return pTaskInfo->code;
×
1752
  }
1753

1754
  if (pTaskInfo->owner != 0) {
9,942,425✔
1755
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1756
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1757

1758
    taosRUnLockLatch(&pTaskInfo->lock);
×
1759
    return pTaskInfo->code;
×
1760
  }
1761

1762
  pTaskInfo->owner = threadId;
9,941,999✔
1763
  taosRUnLockLatch(&pTaskInfo->lock);
9,942,212✔
1764

1765
  if (pTaskInfo->cost.start == 0) {
9,942,425✔
1766
    pTaskInfo->cost.start = taosGetTimestampUs();
229,167✔
1767
  }
1768

1769
  // error occurs, record the error code and return to client
1770
  int32_t ret = setjmp(pTaskInfo->env);
9,942,425✔
1771
  if (ret != TSDB_CODE_SUCCESS) {
11,446,664✔
1772
    pTaskInfo->code = ret;
1,505,086✔
1773
    (void)cleanUpUdfs();
1,505,086✔
1774
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,505,086✔
1775
    atomic_store_64(&pTaskInfo->owner, 0);
1,505,086✔
1776
    return pTaskInfo->code;
1,505,086✔
1777
  }
1778

1779
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,941,578✔
1780

1781
  int64_t st = taosGetTimestampUs();
9,941,999✔
1782

1783
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
9,941,999✔
1784
  if (code) {
8,437,339✔
1785
    pTaskInfo->code = code;
×
1786
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1787
  } else {
1788
    *finished = *ppRes == NULL;
8,437,339✔
1789
    code = blockDataCheck(*ppRes);
8,437,339✔
1790
  }
1791
  if (code) {
8,437,339✔
1792
    pTaskInfo->code = code;
×
1793
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1794
  }
1795

1796
  uint64_t el = (taosGetTimestampUs() - st);
8,437,126✔
1797

1798
  pTaskInfo->cost.elapsedTime += el;
8,437,126✔
1799
  if (NULL == *ppRes) {
8,437,126✔
1800
    *useconds = pTaskInfo->cost.elapsedTime;
5,276,818✔
1801
  }
1802

1803
  (void)cleanUpUdfs();
8,437,126✔
1804

1805
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,437,339✔
1806
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,437,339✔
1807

1808
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,437,339✔
1809
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1810

1811
  atomic_store_64(&pTaskInfo->owner, 0);
8,437,339✔
1812
  return pTaskInfo->code;
8,437,339✔
1813
}
1814

1815
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1816
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1817
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1818
// }
1819

1820
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
245,933✔
1821
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1822
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1823
  int32_t code = 0;                                        
245,933✔
1824
  if (*pTableListInfo != NULL) {
245,933✔
1825
    qDebug("table list already exists, no need to create again");
×
1826
    goto end;
×
1827
  }
1828
  STableListInfo* pList = tableListCreate();
245,933✔
1829
  if (pList == NULL) {
245,933✔
1830
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1831
    code = terrno;
×
1832
    goto end;
×
1833
  }
1834

1835
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
245,933✔
1836
  SReadHandle    pHandle = {.vnode = pVnode};
245,933✔
1837
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
245,933✔
1838

1839
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
245,730✔
1840
  if (code != 0) {
245,933✔
1841
    tableListDestroy(pList);
×
1842
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1843
    goto end;
×
1844
  }
1845
  *pTableListInfo = pList;
245,933✔
1846

1847
end:
245,933✔
1848
  return 0;
245,933✔
1849
}
1850

1851
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
36,747✔
1852
  bool   listAdded = false;
36,747✔
1853
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
36,747✔
1854
  if (code == 0 && !listAdded) {
36,747✔
1855
    int32_t numOfTables = taosArrayGetSize(pUidList);
30,557✔
1856
    for (int i = 0; i < numOfTables; i++) {
90,148✔
1857
      void* tmp = taosArrayGet(pUidList, i);
59,591✔
1858
      if (tmp == NULL) {
59,591✔
1859
        return terrno;
×
1860
      }
1861
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
59,591✔
1862

1863
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
59,591✔
1864
      if (p == NULL) {
59,591✔
1865
        return terrno;
×
1866
      }
1867
    }
1868
  }
1869
  return code;
36,747✔
1870
}
1871

1872
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
28,821✔
1873
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1874
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1875
  int32_t code = TSDB_CODE_SUCCESS;
28,821✔
1876
  STableListInfo* pList = tableListCreate();
28,821✔
1877
  if (pList == NULL) {
28,821✔
1878
    code = terrno;
×
1879
    goto end;
×
1880
  }
1881
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
28,821✔
1882
  if (uidListCopy == NULL) {
28,821✔
1883
    code = terrno;
×
1884
    goto end;
×
1885
  }
1886
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
28,821✔
1887
  SReadHandle    pHandle = {.vnode = pVnode};
28,821✔
1888

1889
  pList->idInfo.suid = suid;
28,821✔
1890
  pList->idInfo.tableType = TD_SUPER_TABLE;
28,821✔
1891
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
28,821✔
1892
  if (code != TSDB_CODE_SUCCESS) {
28,821✔
1893
    goto end;
×
1894
  }                                              
1895
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
28,821✔
1896
  if (code != TSDB_CODE_SUCCESS) {
28,821✔
1897
    goto end;
×
1898
  }
1899
  *tableList = pList->pTableList;
28,821✔
1900
  pList->pTableList = NULL;
28,821✔
1901

1902
  taosArrayClear(uidList);
28,821✔
1903
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
57,642✔
1904
    void* tmp = taosArrayGet(uidListCopy, i);
28,821✔
1905
    if (tmp == NULL) {
28,821✔
1906
      continue;
×
1907
    }
1908
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
28,821✔
1909
    if (slot == NULL) {
28,821✔
1910
      if (taosArrayPush(uidList, tmp) == NULL) {
3,272✔
1911
        code = terrno;
×
1912
        goto end;
×
1913
      }
1914
    }
1915
  }
1916
end:
28,821✔
1917
  taosArrayDestroy(uidListCopy);
28,821✔
1918
  tableListDestroy(pList);
28,821✔
1919
  return code;
28,821✔
1920
}
1921

1922
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1923
//   int32_t index = -1;
1924
//   (void)taosThreadRwlockRdlock(lock);
1925
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1926
//     index = 0;
1927
//     goto end;
1928
//   }
1929
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1930
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1931

1932
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1933
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1934
//       index = i;
1935
//       goto end;
1936
//     }
1937
//   }
1938
// end:
1939
//   (void)taosThreadRwlockUnlock(lock);
1940
//   return index;
1941
// }
1942

1943
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
245,933✔
1944
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
245,933✔
1945
  STableListInfo* pList = pTableListInfo;
245,933✔
1946
  return pList->pTableList;
245,933✔
1947
}
1948

1949
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,420,197✔
1950

1951
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,436,960✔
1952
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,436,960✔
1953
  qDestroyTask(tInfo);
2,436,960✔
1954
}
2,436,960✔
1955

1956
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
597,467✔
1957
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
597,467✔
1958
}
1959

1960
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
643,732✔
1961
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1962
  int32_t      code = 0;
643,732✔
1963
  SNode*       pNode = 0;
643,732✔
1964
  SNodeList*   pList = NULL;
643,732✔
1965
  SExprInfo*   pExprInfo = NULL;
643,732✔
1966
  int32_t      numOfExprs = 1;
643,732✔
1967
  int32_t*     offset = 0;
643,732✔
1968
  STargetNode* pTargetNode = NULL;
643,732✔
1969
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
643,732✔
1970
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
643,732✔
1971

1972
  if (code == 0) {
643,732✔
1973
    pTargetNode->dataBlockId = 0;
643,732✔
1974
    pTargetNode->pExpr = pNode;
643,732✔
1975
    pTargetNode->slotId = 0;
643,732✔
1976
  }
1977
  if (code == 0) {
643,732✔
1978
    code = nodesMakeList(&pList);
643,732✔
1979
  }
1980
  if (code == 0) {
643,732✔
1981
    code = nodesListAppend(pList, (SNode*)pTargetNode);
643,732✔
1982
  }
1983
  if (code == 0) {
643,732✔
1984
    pNode = NULL;
643,732✔
1985
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
643,732✔
1986
  }
1987

1988
  if (code == 0) {
643,732✔
1989
    const char* pVal = NULL;
643,732✔
1990
    int32_t     len = 0;
643,732✔
1991
    SNode*      pSclNode = NULL;
643,732✔
1992
    switch (pExprInfo->pExpr->nodeType) {
643,732✔
1993
      case QUERY_NODE_FUNCTION:
643,732✔
1994
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
643,732✔
1995
        break;
643,732✔
1996
      case QUERY_NODE_OPERATOR:
×
1997
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1998
        break;
×
1999
      default:
×
2000
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2001
        break;
×
2002
    }
2003
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
643,732✔
2004
    SSDataBlock block = {0};
643,732✔
2005
    block.info.rows = 1;
643,732✔
2006
    SSDataBlock* pBlock = &block;
643,732✔
2007
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
643,732✔
2008
    if (tmp == NULL) {
643,732✔
2009
      code = terrno;
×
2010
    }
2011
    if (code == 0) {
643,732✔
2012
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
643,732✔
2013
      gTaskScalarExtra.pStreamRange = NULL;
643,732✔
2014
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
643,732✔
2015
    }
2016
    taosArrayDestroy(pBlockList);
643,732✔
2017
  }
2018
  nodesDestroyList(pList);
643,732✔
2019
  destroyExprInfo(pExprInfo, numOfExprs);
643,732✔
2020
  taosMemoryFreeClear(pExprInfo);
643,732✔
2021
  return code;
643,732✔
2022
}
2023

2024
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,119,662✔
2025
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,119,662✔
2026
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,119,662✔
2027
  int32_t        code = 0;
2,119,662✔
2028
  SNode*         pNode = NULL;
2,119,662✔
2029
  if (!pForceOutputCols) return 0;
2,119,662✔
2030
  if (!*pRes) {
46,265✔
2031
    code = createDataBlock(pRes);
46,265✔
2032
  }
2033

2034
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
46,265✔
2035
    int32_t idx = 0;
46,265✔
2036
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
184,875✔
2037
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
138,610✔
2038
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
138,610✔
2039
      colInfo.info.precision = pCol->type.precision;
138,610✔
2040
      colInfo.info.scale = pCol->type.scale;
138,610✔
2041
      code = blockDataAppendColInfo(*pRes, &colInfo);
138,610✔
2042
      if (code != 0) break;
138,610✔
2043
    }
2044
  }
2045

2046
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
46,265✔
2047
  if (code != TSDB_CODE_SUCCESS) {
46,265✔
2048
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2049
    return code;
×
2050
  }
2051

2052
  // loop all exprs for force output, execute all exprs
2053
  int32_t idx = 0;
46,265✔
2054
  int32_t rowIdx = (*pRes)->info.rows;
46,265✔
2055
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
46,265✔
2056
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
46,265✔
2057
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
184,875✔
2058
    SScalarParam   dst = {0};
138,610✔
2059
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
138,610✔
2060
    code = nodesStringToNode(pCol->expr, &pNode);
138,610✔
2061
    if (code != 0) break;
138,610✔
2062
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
138,610✔
2063
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
138,610✔
2064
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
92,345✔
2065
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
92,345✔
2066
    } else {
2067
      dst.columnData = pInfo;
46,265✔
2068
      dst.numOfRows = rowIdx;
46,265✔
2069
      dst.colAlloced = false;
46,265✔
2070
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
46,265✔
2071
    }
2072
    ++idx;
138,610✔
2073
    // TODO sclFreeParam(&dst);
2074
    nodesDestroyNode(pNode);
138,610✔
2075
    if (code != 0) break;
138,610✔
2076
  }
2077
  if (code == TSDB_CODE_SUCCESS) {
46,265✔
2078
    (*pRes)->info.rows++;
46,265✔
2079
  }
2080
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
46,265✔
2081
  return code;
46,265✔
2082
}
2083

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

2124
      pCol->hasNull = true;
233,570✔
2125
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
233,570✔
2126
      pCol->info.colId = 0;
233,570✔
2127
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
233,570✔
2128
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
233,570✔
2129
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
233,570✔
2130
      code = colInfoDataEnsureCapacity(pCol, 1, true);
233,570✔
2131
      if (code != 0) {
233,570✔
2132
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2133
        taosMemoryFree(pCol);
×
2134
        break;
×
2135
      }
2136
      dst.columnData = pCol;
233,570✔
2137
      dst.numOfRows = 1;
233,570✔
2138
      dst.colAlloced = true;
233,570✔
2139
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
233,570✔
2140
      if (colDataIsNull_var(dst.columnData, 0)) {
233,570✔
2141
        qInfo("invalid sub tb expr with null value");
3,120✔
2142
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,120✔
2143
      }
2144
      if (code == 0) {
233,570✔
2145
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
230,450✔
2146
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
230,450✔
2147
      }
2148
    } break;
233,570✔
2149
    default:
×
2150
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2151
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2152
      break;
×
2153
  }
2154
  if (code == 0) {
233,570✔
2155
    if (!pVal || len == 0) {
230,450✔
2156
      qError("tbname generated with no characters which is not allowed");
×
2157
      code = TSDB_CODE_INVALID_PARA;
×
2158
    }
2159
    if(len > TSDB_TABLE_NAME_LEN - 1) {
230,450✔
2160
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
426✔
2161
      len = TSDB_TABLE_NAME_LEN - 1;
426✔
2162
    }
2163

2164
    memcpy(tbname, pVal, len);
230,450✔
2165
    tbname[len] = '\0';  // ensure null terminated
230,450✔
2166
    if (NULL != strchr(tbname, '.')) {
230,450✔
2167
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2168
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2169
    }
2170
  }
2171
  // TODO free dst
2172
  sclFreeParam(&dst);
233,570✔
2173
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
233,570✔
2174
  return code;
233,570✔
2175
}
2176

2177
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
222,688✔
2178
  if (pParam) {
222,688✔
2179
    if (pParam->tbname) {
222,688✔
2180
      taosMemFree(pParam->tbname);
222,688✔
2181
      pParam->tbname = NULL;
222,688✔
2182
    }
2183
    if (pParam->stbname) {
222,688✔
2184
      taosMemFree(pParam->stbname);
222,688✔
2185
      pParam->stbname = NULL;
222,688✔
2186
    }
2187
    if (pParam->dbFName) {
222,688✔
2188
      taosMemFree(pParam->dbFName);
222,688✔
2189
      pParam->dbFName = NULL;
222,688✔
2190
    }
2191
    if (pParam->pFields) {
222,688✔
2192
      taosArrayDestroy(pParam->pFields);
222,688✔
2193
      pParam->pFields = NULL;
222,688✔
2194
    }
2195
    if (pParam->pTagFields) {
222,688✔
2196
      taosArrayDestroy(pParam->pTagFields);
123,827✔
2197
      pParam->pTagFields = NULL;
123,827✔
2198
    }
2199
    if (pParam->colCids) {
222,688✔
2200
      taosArrayDestroy(pParam->colCids);
1,720✔
2201
      pParam->colCids = NULL;
1,720✔
2202
    }
2203
    if (pParam->tagCids) {
222,688✔
2204
      taosArrayDestroy(pParam->tagCids);
1,097✔
2205
      pParam->tagCids = NULL;
1,097✔
2206
    }
2207
    taosMemFree(pParam);
222,688✔
2208
  }
2209
}
222,688✔
2210

2211
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
222,688✔
2212
  int32_t code = 0, lino = 0;
222,688✔
2213
  if (ppDst == NULL || pSrc == NULL) {
222,688✔
2214
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2215
  }
2216
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
222,688✔
2217
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
222,475✔
2218

2219
  (*ppDst)->suid = pSrc->suid;
222,475✔
2220
  (*ppDst)->sver = pSrc->sver;
222,688✔
2221
  (*ppDst)->tbType = pSrc->tbType;
222,688✔
2222
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
222,688✔
2223
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
222,688✔
2224

2225
  if (pSrc->stbname) {
222,688✔
2226
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
222,688✔
2227
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
222,688✔
2228
  }
2229

2230
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
222,475✔
2231
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
222,688✔
2232

2233
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
222,267✔
2234

2235
  if (pSrc->pFields && pSrc->pFields->size > 0) {
222,688✔
2236
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
222,688✔
2237
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
222,688✔
2238
  } else {
2239
    (*ppDst)->pFields = NULL;
×
2240
  }
2241
  
2242
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
222,688✔
2243
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
123,827✔
2244
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
123,827✔
2245
  } else {
2246
    (*ppDst)->pTagFields = NULL;
98,861✔
2247
  }
2248

2249
  if (pSrc->colCids && pSrc->colCids->size > 0) {
222,688✔
2250
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,720✔
2251
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,720✔
2252
  } else {
2253
    (*ppDst)->colCids = NULL;
220,968✔
2254
  }
2255

2256
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
222,688✔
2257
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,097✔
2258
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,097✔
2259
  } else {
2260
    (*ppDst)->tagCids = NULL;
221,591✔
2261
  }
2262

2263
_exit:
222,688✔
2264

2265
  if (code != 0) {
222,688✔
2266
    if (*ppDst) {
×
2267
      destroyStreamInserterParam(*ppDst);
×
2268
      *ppDst = NULL;
×
2269
    }
2270
    
2271
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2272
  }
2273
  return code;
222,688✔
2274
}
2275

2276
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
245✔
2277
  return doDropStreamTable(pMsgCb, pOutput, pReq);
245✔
2278
}
2279

2280
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2281
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2282
}
2283

2284
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
7,926✔
2285
  int32_t         code = TSDB_CODE_SUCCESS;
7,926✔
2286
  STableListInfo* pList = tableListCreate();
7,926✔
2287
  if (pList == NULL) {
7,926✔
2288
    code = terrno;
×
2289
    goto end;
×
2290
  }
2291
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2292
  // if (uidListCopy == NULL) {
2293
  //   code = terrno;
2294
  //   goto end;
2295
  // }
2296

2297
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
7,926✔
2298
  pList->idInfo.suid = suid;
7,926✔
2299
  pList->idInfo.tableType = TD_SUPER_TABLE;
7,926✔
2300
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
7,926✔
2301
  if (code != TSDB_CODE_SUCCESS) {
7,926✔
2302
    goto end;
×
2303
  }
2304
  // taosArrayClear(uidList);
2305
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2306
  //   void* tmp = taosArrayGet(uidListCopy, i);
2307
  //   if (tmp == NULL) {
2308
  //     continue;
2309
  //   }
2310
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2311
  //   if (slot == NULL) {
2312
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2313
  //       code = terrno;
2314
  //       goto end;
2315
  //     }
2316
  //   }
2317
  // }
2318
end:
7,926✔
2319
  // taosArrayDestroy(uidListCopy);
2320
  tableListDestroy(pList);
7,926✔
2321
  return code;
7,926✔
2322
}
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