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

taosdata / TDengine / #4928

15 Jan 2026 04:09PM UTC coverage: 66.708% (+0.6%) from 66.12%
#4928

push

travis-ci

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

840 of 1255 new or added lines in 23 files covered. (66.93%)

826 existing lines in 119 files now uncovered.

203035 of 304362 relevant lines covered (66.71%)

130886757.17 hits per line

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

69.64
/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) {
927,387,793✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
927,387,793✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
927,387,793✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
927,733,735✔
47
}
927,327,422✔
48

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

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

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

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

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
42,999,959✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
42,999,959✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
43,001,194✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
19,439,618✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
19,440,484✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
19,440,461✔
162
      if (pScanInfo->base.dataReader != NULL) {
19,440,794✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
263,458✔
164
        if (code) {
263,160✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
23,558,843✔
172
  }
173

174
  return 0;
23,066,621✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
23,066,974✔
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) {
430,181✔
213
  if (msg == NULL) {  // create raw scan
430,181✔
214
    SExecTaskInfo* pTaskInfo = NULL;
108,172✔
215

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

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

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

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

248
  return pTaskInfo;
322,880✔
249
}
250

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

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

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

272
  return TSDB_CODE_SUCCESS;
211,810✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
211,810✔
323
    if (code) {
211,810✔
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,
505,781✔
328
         tstrerror(code));
329

330
_error:
74,430✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,120,242✔
342
  if (pInfo == NULL) {
3,120,242✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,120,242✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,120,242✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,120,242✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,120,242✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,869,218✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
525,680✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,989,460✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

377
  void*           info = pOperator->info;
3,120,242✔
378
  STableScanBase* pScanBaseInfo = NULL;
3,120,242✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,120,242✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
251,024✔
382
    setReadHandle(handle, pScanBaseInfo);
251,024✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,869,218✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,343,538✔
385
    setReadHandle(handle, pScanBaseInfo);
2,343,538✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
506,597✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
383,700✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
383,700✔
444

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

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

454
  locked = 1;
383,700✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
821,976✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
438,276✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
438,276✔
458

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

465
    tDecoderClear(&mr.coder);
438,276✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
438,276✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
438,276✔
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) {
438,276✔
473
          continue;
3,420✔
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};
434,856✔
485
    if (pScanInfo->pTagCond != NULL) {
434,856✔
486
      // tb_uid_t id = mr.me.uid;
487
      item.check = 1;
337,800✔
488
    }
489
    if (taosArrayPush(tUid, &item) == NULL) {
434,856✔
490
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
491
    }
492
  }
493

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
383,700✔
524

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

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

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

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

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
385,699✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
385,699✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
385,699✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
385,699✔
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
385,699✔
566
    SArray* qa = NULL;
383,700✔
567
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
383,700✔
568
    if (code != TSDB_CODE_SUCCESS) {
383,700✔
569
      taosArrayDestroy(qa);
×
570
      return code;
×
571
    }
572
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
383,700✔
573
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
383,700✔
574
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
383,700✔
575

576
    bool   assignUid = false;
383,700✔
577
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
383,700✔
578
    char*  keyBuf = NULL;
383,700✔
579
    if (bufLen > 0) {
383,700✔
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;
383,700✔
589
    taosWLockLatch(&pTaskInfo->lock);
383,700✔
590

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

601
      if (bufLen > 0) {
265,956✔
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);
265,956✔
617
      if (code != TSDB_CODE_SUCCESS) {
265,956✔
618
        taosMemoryFree(keyBuf);
×
619
        taosArrayDestroy(qa);
×
620
        taosWUnLockLatch(&pTaskInfo->lock);
×
621
        return code;
×
622
      }
623
    }
624

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

630
    taosArrayDestroy(qa);
383,700✔
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);
1,999✔
633
    taosWLockLatch(&pTaskInfo->lock);
1,999✔
634
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,999✔
635
    taosWUnLockLatch(&pTaskInfo->lock);
1,999✔
636
  }
637

638
  return code;
385,699✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
712,172,318✔
647
    return TSDB_CODE_INVALID_PARA;
30,175✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
712,144,070✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
712,144,070✔
652
    return TSDB_CODE_SUCCESS;
411,152,955✔
653
  }
654

655
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
301,538,945✔
656
  if (!pSchemaInfo) {
301,043,979✔
657
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
658
    return terrno;
×
659
  }
660

661
  *sversion = pSchemaInfo->sw->version;
301,043,979✔
662
  *tversion = pSchemaInfo->tversion;
301,771,910✔
663
  *rversion = pSchemaInfo->rversion;
301,304,683✔
664
  if (pSchemaInfo->dbname) {
301,159,990✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
301,350,460✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
302,034,203✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
301,682,358✔
671
  } else {
672
    tableName[0] = 0;
20,867✔
673
  }
674

675
  *tbGet = true;
302,027,899✔
676

677
  return TSDB_CODE_SUCCESS;
301,814,729✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
410,891,905✔
681

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

689
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
79,969,088✔
690
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
79,969,088✔
691
  ((SExecTaskInfo*)tinfo)->paramSet = false;
79,971,618✔
692
}
79,971,618✔
693

694
int32_t qExecutorInit(void) {
4,196,468✔
695
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,196,468✔
696
  return TSDB_CODE_SUCCESS;
4,196,740✔
697
}
698

699
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
70,391,965✔
700
  int32_t        code = TSDB_CODE_SUCCESS;
70,391,965✔
701
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
70,391,965✔
702
  if (pTask->pWorkerCb) {
70,391,965✔
703
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
70,409,612✔
704
    if (code != TSDB_CODE_SUCCESS) {
70,454,069✔
705
      pTask->code = code;
×
706
      return pTask->code;
×
707
    }
708
  }
709

710
  code = tsem_wait(pSem);
70,485,868✔
711
  if (code != TSDB_CODE_SUCCESS) {
70,457,359✔
712
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
713
    pTask->code = code;
×
714
    return pTask->code;
×
715
  }
716

717
  if (pTask->pWorkerCb) {
70,457,359✔
718
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
70,460,595✔
719
    if (code != TSDB_CODE_SUCCESS) {
70,463,583✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
70,461,269✔
725
}
726

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

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

736
  readHandle->uid = 0;
424,490,434✔
737
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
424,530,093✔
738
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
424,058,210✔
739
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
11,918,407✔
740
    goto _error;
11,440,426✔
741
  }
742
    
743
  if (handle) {
412,303,084✔
744
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
412,225,555✔
745
    void*           pSinkManager = NULL;
412,379,985✔
746
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
412,116,760✔
747
    if (code != TSDB_CODE_SUCCESS) {
411,604,235✔
748
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
749
      goto _error;
×
750
    }
751

752
    void* pSinkParam = NULL;
411,604,235✔
753
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
411,558,758✔
754
    if (code != TSDB_CODE_SUCCESS) {
411,792,318✔
755
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
756
      taosMemoryFree(pSinkManager);
×
757
      goto _error;
×
758
    }
759

760
    SDataSinkNode* pSink = NULL;
411,792,318✔
761
    if (readHandle->localExec) {
411,974,694✔
762
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,728✔
763
      if (code != TSDB_CODE_SUCCESS) {
2,728✔
764
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
765
               (*pTask)->id.str);
766
        taosMemoryFree(pSinkManager);
×
767
        goto _error;
×
768
      }
769
    }
770

771
    // pSinkParam has been freed during create sinker.
772
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
411,567,788✔
773
                              (*pTask)->id.str, pSubplan->processOneBlock);
412,402,657✔
774
    if (code) {
411,623,365✔
775
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
582✔
776
    }
777
  }
778

779
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
412,485,801✔
780
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
781

782
_error:
210,557,036✔
783
  // if failed to add ref for all tables in this query, abort current query
784
  return code;
424,134,213✔
785
}
786

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

792
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
506,107,601✔
793
                     bool processOneBlock) {
794
  int32_t        code = TSDB_CODE_SUCCESS;
506,107,601✔
795
  int32_t        lino = 0;
506,107,601✔
796
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
506,107,601✔
797
  int64_t        threadId = taosGetSelfPthreadId();
506,107,601✔
798

799
  if (pLocal) {
505,989,348✔
800
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
502,655,207✔
801
  }
802

803
  taosArrayClear(pResList);
505,582,572✔
804

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

812
  if (pTaskInfo->cost.start == 0) {
505,734,843✔
813
    pTaskInfo->cost.start = taosGetTimestampUs();
406,819,755✔
814
  }
815

816
  if (isTaskKilled(pTaskInfo)) {
506,140,439✔
817
    atomic_store_64(&pTaskInfo->owner, 0);
461✔
818
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
461✔
819
    return pTaskInfo->code;
461✔
820
  }
821

822
  // error occurs, record the error code and return to client
823
  int32_t ret = setjmp(pTaskInfo->env);
505,826,313✔
824
  if (ret != TSDB_CODE_SUCCESS) {
516,207,731✔
825
    pTaskInfo->code = ret;
11,065,262✔
826
    (void)cleanUpUdfs();
11,065,262✔
827

828
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
11,065,262✔
829
    atomic_store_64(&pTaskInfo->owner, 0);
11,065,262✔
830

831
    return pTaskInfo->code;
11,065,262✔
832
  }
833

834
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
505,142,469✔
835

836
  int32_t      current = 0;
505,144,332✔
837
  SSDataBlock* pRes = NULL;
505,144,332✔
838
  int64_t      st = taosGetTimestampUs();
506,148,628✔
839

840
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
506,148,628✔
841
    pTaskInfo->paramSet = true;
79,964,184✔
842
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
79,961,755✔
843
  } else {
844
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
425,995,945✔
845
  }
846

847
  QUERY_CHECK_CODE(code, lino, _end);
495,299,125✔
848
  code = blockDataCheck(pRes);
495,299,125✔
849
  QUERY_CHECK_CODE(code, lino, _end);
495,391,114✔
850

851
  if (pRes == NULL) {
495,391,114✔
852
    st = taosGetTimestampUs();
102,824,280✔
853
  }
854

855
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
495,381,380✔
856
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
495,388,576✔
857
    rowsThreshold = 4096;
494,727,253✔
858
  }
859

860
  int32_t blockIndex = 0;
495,335,905✔
861
  while (pRes != NULL) {
1,131,196,844✔
862
    SSDataBlock* p = NULL;
684,049,067✔
863
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
683,931,966✔
864
      SSDataBlock* p1 = NULL;
527,356,700✔
865
      code = createOneDataBlock(pRes, true, &p1);
527,380,380✔
866
      QUERY_CHECK_CODE(code, lino, _end);
527,347,366✔
867

868
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
527,347,366✔
869
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
527,466,802✔
870
      p = p1;
527,466,802✔
871
    } else {
872
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
156,802,177✔
873
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
156,802,647✔
874

875
      p = *(SSDataBlock**)tmp;
156,802,647✔
876
      code = copyDataBlock(p, pRes);
156,802,647✔
877
      QUERY_CHECK_CODE(code, lino, _end);
156,803,424✔
878
    }
879

880
    blockIndex += 1;
684,243,808✔
881

882
    current += p->info.rows;
684,243,808✔
883
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
684,180,180✔
884
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
885
    void* tmp = taosArrayPush(pResList, &p);
684,234,708✔
886
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
684,234,708✔
887

888
    if (current >= rowsThreshold || processOneBlock) {
684,234,708✔
889
      break;
890
    }
891

892
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
635,948,471✔
893
    QUERY_CHECK_CODE(code, lino, _end);
635,943,193✔
894
    code = blockDataCheck(pRes);
635,943,193✔
895
    QUERY_CHECK_CODE(code, lino, _end);
635,990,296✔
896
  }
897

898
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
495,434,014✔
899
    pTaskInfo->pSubplan->rowsThreshold -= current;
525,776✔
900
  }
901

902
  *hasMore = (pRes != NULL);
495,457,881✔
903
  uint64_t el = (taosGetTimestampUs() - st);
495,394,892✔
904

905
  pTaskInfo->cost.elapsedTime += el;
495,394,892✔
906
  if (NULL == pRes) {
495,314,436✔
907
    *useconds = pTaskInfo->cost.elapsedTime;
447,028,199✔
908
  }
909

910
_end:
495,305,603✔
911
  (void)cleanUpUdfs();
495,284,640✔
912

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

917
  atomic_store_64(&pTaskInfo->owner, 0);
495,576,012✔
918
  if (code) {
495,574,481✔
919
    pTaskInfo->code = code;
×
920
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
921
  }
922

923
  return pTaskInfo->code;
495,574,481✔
924
}
925

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

937
  taosArrayClear(pTaskInfo->pResultBlockList);
×
938
}
×
939

940
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
119,690,258✔
941
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
119,690,258✔
942
  int64_t        threadId = taosGetSelfPthreadId();
119,690,258✔
943
  int64_t        curOwner = 0;
119,690,868✔
944

945
  *pRes = NULL;
119,690,868✔
946

947
  // todo extract method
948
  taosRLockLatch(&pTaskInfo->lock);
119,690,868✔
949
  bool isKilled = isTaskKilled(pTaskInfo);
119,692,094✔
950
  if (isKilled) {
119,692,094✔
951
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
952

953
    taosRUnLockLatch(&pTaskInfo->lock);
×
954
    return pTaskInfo->code;
×
955
  }
956

957
  if (pTaskInfo->owner != 0) {
119,692,094✔
958
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
959
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
960

961
    taosRUnLockLatch(&pTaskInfo->lock);
×
962
    return pTaskInfo->code;
×
963
  }
964

965
  pTaskInfo->owner = threadId;
119,691,754✔
966
  taosRUnLockLatch(&pTaskInfo->lock);
119,691,200✔
967

968
  if (pTaskInfo->cost.start == 0) {
119,692,094✔
969
    pTaskInfo->cost.start = taosGetTimestampUs();
226,878✔
970
  }
971

972
  // error occurs, record the error code and return to client
973
  int32_t ret = setjmp(pTaskInfo->env);
119,688,431✔
974
  if (ret != TSDB_CODE_SUCCESS) {
119,687,591✔
975
    pTaskInfo->code = ret;
×
976
    (void)cleanUpUdfs();
×
977
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
978
    atomic_store_64(&pTaskInfo->owner, 0);
×
979
    return pTaskInfo->code;
×
980
  }
981

982
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
119,687,591✔
983

984
  int64_t st = taosGetTimestampUs();
119,689,084✔
985
  int32_t code = TSDB_CODE_SUCCESS;
119,689,084✔
986
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
119,689,084✔
987
    pTaskInfo->paramSet = true;
×
988
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
989
  } else {
990
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
119,688,736✔
991
  }
992
  if (code) {
119,639,798✔
993
    pTaskInfo->code = code;
×
994
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
995
  }
996

997
  code = blockDataCheck(*pRes);
119,639,798✔
998
  if (code) {
119,687,210✔
999
    pTaskInfo->code = code;
×
1000
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1001
  }
1002

1003
  uint64_t el = (taosGetTimestampUs() - st);
119,648,578✔
1004

1005
  pTaskInfo->cost.elapsedTime += el;
119,648,578✔
1006
  if (NULL == *pRes) {
119,648,446✔
1007
    *useconds = pTaskInfo->cost.elapsedTime;
18,980,288✔
1008
  }
1009

1010
  (void)cleanUpUdfs();
119,665,576✔
1011

1012
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
119,692,094✔
1013
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
119,692,094✔
1014

1015
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
119,692,094✔
1016
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1017

1018
  atomic_store_64(&pTaskInfo->owner, 0);
119,692,434✔
1019
  return pTaskInfo->code;
119,691,754✔
1020
}
1021

1022
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
128,026,472✔
1023
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
128,026,472✔
1024
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
128,027,886✔
1025
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
128,029,739✔
1026

1027
  if (!tmp) {
128,025,080✔
1028
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1029
    return terrno;
×
1030
  }
1031
  return TSDB_CODE_SUCCESS;
128,025,080✔
1032
}
1033

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

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

1044
  return 0;
×
1045
}
1046

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

1056
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
364,282✔
1057
  if (pTaskInfo->subJobCtx.hasSubJobs) {
364,282✔
1058
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
325,007✔
1059
    if (pTaskInfo->subJobCtx.param) {
325,007✔
1060
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
159,254✔
1061
    }
1062
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
325,007✔
1063
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
325,007✔
1064
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
325,007✔
1065
  }
1066
  
1067
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
364,282✔
1068

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

1091
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
364,282✔
1092
}
364,282✔
1093

1094
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
364,282✔
1095
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
364,282✔
1096
  if (pTaskInfo == NULL) {
364,282✔
1097
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1098
  }
1099

1100
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
364,282✔
1101

1102
  setTaskKilled(pTaskInfo, rspCode);
364,282✔
1103
  qStopTaskOperators(pTaskInfo);
364,282✔
1104

1105
  return TSDB_CODE_SUCCESS;
364,282✔
1106
}
1107

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

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

1121
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1122

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

1129
        taosMsleep(200);
×
1130

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

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

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

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

1161
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
413,788,286✔
1162
  STaskCostInfo* pSummary = &pTaskInfo->cost;
413,788,286✔
1163
  int64_t        idleTime = pSummary->start - pSummary->created;
413,833,830✔
1164

1165
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
413,832,305✔
1166
  if (pSummary->pRecoder != NULL) {
413,807,173✔
1167
    qDebug(
296,420,418✔
1168
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1169
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1170
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1171
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1172
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1173
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1174
  } else {
1175
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
117,278,308✔
1176
           pSummary->elapsedTime / 1000.0);
1177
  }
1178
}
413,699,075✔
1179

1180

1181
void qDestroyTask(qTaskInfo_t qTaskHandle) {
430,693,704✔
1182
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
430,693,704✔
1183
  if (pTaskInfo == NULL) {
430,693,704✔
1184
    return;
16,926,356✔
1185
  }
1186

1187
  if (pTaskInfo->pRoot != NULL) {
413,767,348✔
1188
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
413,884,270✔
1189
  } else {
1190
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1191
  }
1192

1193
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
413,885,175✔
1194
  doDestroyTask(pTaskInfo);
413,761,720✔
1195
}
1196

1197
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
13,673,589✔
1198
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
13,673,589✔
1199
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
13,673,589✔
1200
}
1201

1202
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
322,880✔
1203
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
322,880✔
1204
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
322,880✔
1205

1206
  while (1) {
316,962✔
1207
    uint16_t type = pOperator->operatorType;
639,842✔
1208
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
639,842✔
1209
      *scanner = pOperator->info;
322,880✔
1210
      break;
322,880✔
1211
    } else {
1212
      pOperator = pOperator->pDownstream[0];
316,962✔
1213
    }
1214
  }
1215
}
322,880✔
1216

1217
void* qExtractReaderFromTmqScanner(void* scanner) {
322,880✔
1218
  SStreamScanInfo* pInfo = scanner;
322,880✔
1219
  return (void*)pInfo->tqReader;
322,880✔
1220
}
1221

1222
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
730,642✔
1223
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
730,642✔
1224
  return pTaskInfo->streamInfo.schema;
730,642✔
1225
}
1226

1227
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
730,642✔
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
730,642✔
1229
  return pTaskInfo->streamInfo.tbName;
730,642✔
1230
}
1231

1232
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
767,622✔
1233
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
767,622✔
1234
  return &pTaskInfo->streamInfo.btMetaRsp;
767,622✔
1235
}
1236

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

1248
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
728,564✔
1249
  memset(pCond, 0, sizeof(SQueryTableDataCond));
728,564✔
1250
  pCond->order = TSDB_ORDER_ASC;
728,564✔
1251
  pCond->numOfCols = pMtInfo->schema->nCols;
729,136✔
1252
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
729,144✔
1253
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
729,152✔
1254
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
729,434✔
1255
    taosMemoryFreeClear(pCond->colList);
1,168✔
1256
    taosMemoryFreeClear(pCond->pSlotList);
×
1257
    return terrno;
×
1258
  }
1259

1260
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
729,144✔
1261
  pCond->suid = pMtInfo->suid;
729,434✔
1262
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
729,426✔
1263
  pCond->startVersion = -1;
730,014✔
1264
  pCond->endVersion = sContext->snapVersion;
729,144✔
1265

1266
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,608,708✔
1267
    SColumnInfo* pColInfo = &pCond->colList[i];
3,876,656✔
1268
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,877,236✔
1269
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,878,686✔
1270
    if (pMtInfo->pExtSchemas != NULL) {
3,878,984✔
1271
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,200✔
1272
    }
1273
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,879,854✔
1274
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,878,396✔
1275

1276
    pCond->pSlotList[i] = i;
3,879,564✔
1277
  }
1278

1279
  return TSDB_CODE_SUCCESS;
730,312✔
1280
}
1281

1282
void qStreamSetOpen(qTaskInfo_t tinfo) {
118,190,658✔
1283
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
118,190,658✔
1284
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
118,190,658✔
1285
  pOperator->status = OP_NOT_OPENED;
118,213,843✔
1286
}
118,212,408✔
1287

1288
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
19,258,972✔
1289
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,258,972✔
1290
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
19,258,972✔
1291
}
19,259,861✔
1292

1293
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
20,219,502✔
1294
  int32_t        code = TSDB_CODE_SUCCESS;
20,219,502✔
1295
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,219,502✔
1296
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
20,219,502✔
1297

1298
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
20,221,057✔
1299
  const char*    id = GET_TASKID(pTaskInfo);
20,218,885✔
1300

1301
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
20,217,592✔
1302
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
18,959,875✔
1303
    if (pOperator == NULL || code != 0) {
18,961,093✔
UNCOV
1304
      return code;
×
1305
    }
1306

1307
    SStreamScanInfo* pInfo = pOperator->info;
18,961,093✔
1308
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
18,960,773✔
1309
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
18,959,285✔
1310
    walReaderVerifyOffset(pWalReader, pOffset);
18,961,529✔
1311
  }
1312
  // if pOffset equal to current offset, means continue consume
1313
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
20,217,826✔
1314
    return 0;
18,355,417✔
1315
  }
1316

1317
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,858,780✔
1318
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,113,875✔
1319
    if (pOperator == NULL || code != 0) {
1,114,104✔
1320
      return code;
272✔
1321
    }
1322

1323
    SStreamScanInfo* pInfo = pOperator->info;
1,113,875✔
1324
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,113,875✔
1325
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,113,514✔
1326
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,111,962✔
1327

1328
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,113,209✔
1329
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
899,183✔
1330
      pScanBaseInfo->dataReader = NULL;
899,223✔
1331

1332
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
899,223✔
1333
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
898,539✔
1334
      walReaderVerifyOffset(pWalReader, pOffset);
898,244✔
1335
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
900,153✔
1336
      if (code < 0) {
899,807✔
1337
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,746✔
1338
        return code;
12,092✔
1339
      }
1340
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
212,786✔
1341
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1342
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1343
      int64_t uid = pOffset->uid;
214,297✔
1344
      int64_t ts = pOffset->ts;
213,371✔
1345
      int32_t index = 0;
213,399✔
1346

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

1357
      if (uid == 0) {
213,712✔
1358
        if (numOfTables != 0) {
206,673✔
1359
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,437✔
1360
          if (!tmp) {
38,437✔
1361
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1362
            taosRUnLockLatch(&pTaskInfo->lock);
×
1363
            return terrno;
×
1364
          }
1365
          if (tmp) uid = tmp->uid;
38,437✔
1366
          ts = INT64_MIN;
38,437✔
1367
          pScanInfo->currentTable = 0;
38,437✔
1368
        } else {
1369
          taosRUnLockLatch(&pTaskInfo->lock);
168,236✔
1370
          qError("no table in table list, %s", id);
168,508✔
1371
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
168,508✔
1372
        }
1373
      }
1374
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,476✔
1375

1376
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
45,789✔
1377
             pInfo->pTableScanOp->resultInfo.totalRows);
1378
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
45,789✔
1379

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

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

1394
      STableKeyInfo keyInfo = {.uid = uid};
45,789✔
1395
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
45,789✔
1396

1397
      // let's start from the next ts that returned to consumer.
1398
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
45,789✔
1399
        pScanBaseInfo->cond.twindows.skey = ts;
898✔
1400
      } else {
1401
        pScanBaseInfo->cond.twindows.skey = ts + 1;
44,891✔
1402
      }
1403
      pScanInfo->scanTimes = 0;
45,789✔
1404

1405
      if (pScanBaseInfo->dataReader == NULL) {
45,789✔
1406
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
78,750✔
1407
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1408
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
39,375✔
1409
        if (code != TSDB_CODE_SUCCESS) {
39,375✔
1410
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1411
          return code;
×
1412
        }
1413

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

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

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

1439
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1440
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
744,905✔
1441
      SStreamRawScanInfo* pInfo = pOperator->info;
731,132✔
1442
      SSnapContext*       sContext = pInfo->sContext;
731,132✔
1443
      SOperatorInfo*      p = NULL;
731,132✔
1444

1445
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
731,132✔
1446
      if (code != 0) {
731,132✔
1447
        return code;
×
1448
      }
1449

1450
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
731,132✔
1451

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

1457
      SMetaTableInfo mtInfo = {0};
731,132✔
1458
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
731,132✔
1459
      if (code != 0) {
730,842✔
1460
        destroyMetaTableInfo(&mtInfo);
1461
        return code;
×
1462
      }
1463
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
730,842✔
1464
      pInfo->dataReader = NULL;
729,956✔
1465

1466
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
730,246✔
1467
      tableListClear(pTableListInfo);
729,666✔
1468

1469
      if (mtInfo.uid == 0) {
730,834✔
1470
        destroyMetaTableInfo(&mtInfo);
1471
        goto end;  // no data
820✔
1472
      }
1473

1474
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
730,014✔
1475
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
729,434✔
1476
      if (code != TSDB_CODE_SUCCESS) {
729,434✔
1477
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1478
        destroyMetaTableInfo(&mtInfo);
1479
        return code;
×
1480
      }
1481
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
729,434✔
1482
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,282✔
1483
      } else {
1484
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
727,862✔
1485
      }
1486

1487
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
729,144✔
1488
      if (code != TSDB_CODE_SUCCESS) {
730,312✔
1489
        destroyMetaTableInfo(&mtInfo);
1490
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1491
        return code;
×
1492
      }
1493

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

1508
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,460,624✔
1509
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
730,312✔
1510
      if (code != TSDB_CODE_SUCCESS) {
728,564✔
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
1513
        return code;
×
1514
      }
1515

1516
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
728,564✔
1517
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
729,144✔
1518
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1519
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
727,976✔
1520
      pTaskInfo->streamInfo.schema = mtInfo.schema;
728,854✔
1521
      taosMemoryFreeClear(mtInfo.pExtSchemas);
728,854✔
1522

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

1542
end:
1,678,649✔
1543
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,678,471✔
1544
  return 0;
1,678,465✔
1545
}
1546

1547
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
378,403,184✔
1548
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
378,403,184✔
1549
  if (pMsg->info.ahandle == NULL) {
378,516,037✔
1550
    rpcFreeCont(pMsg->pCont);
566✔
1551
    qError("pMsg->info.ahandle is NULL");
566✔
1552
    return;
566✔
1553
  }
1554

1555
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
378,323,124✔
1556
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1557

1558
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
378,332,566✔
1559

1560
  if (pMsg->contLen > 0) {
378,400,094✔
1561
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
378,326,401✔
1562
    if (buf.pData == NULL) {
378,144,273✔
1563
      pMsg->code = terrno;
×
1564
    } else {
1565
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
378,144,273✔
1566
    }
1567
  }
1568

1569
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
378,418,428✔
1570
  rpcFreeCont(pMsg->pCont);
378,595,658✔
1571
  destroySendMsgInfo(pSendInfo);
378,478,558✔
1572
}
1573

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

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

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

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

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

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

1602
  taosArrayDestroy(plist);
×
1603

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

1612
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,298,160✔
1613
  int32_t        code = TSDB_CODE_SUCCESS;
3,298,160✔
1614
  int32_t        lino = 0;
3,298,160✔
1615
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,298,160✔
1616

1617
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,298,160✔
1618
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1619
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1620

1621
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1622
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1623
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,296,588✔
1624
    STableScanInfo* pScanInfo = pOperator->info;
1,647,233✔
1625

1626
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,647,233✔
1627
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,648,831✔
1628
  } else {
1629
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,649,879✔
1630
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,648,294✔
1631
    }
1632
  }
1633

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

1641
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,650,403✔
1642
  if (pList == NULL) {
1,650,403✔
1643
    return TSDB_CODE_INVALID_PARA;
×
1644
  }
1645

1646
  *pList = NULL;
1,650,403✔
1647
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,650,403✔
1648
  if (pArray == NULL) {
1,650,403✔
1649
    return terrno;
×
1650
  }
1651

1652
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,650,403✔
1653
  if (code == 0) {
1,648,831✔
1654
    *pList = pArray;
1,648,831✔
1655
  } else {
1656
    taosArrayDestroy(pArray);
×
1657
  }
1658
  return code;
1,648,831✔
1659
}
1660

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

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

1677
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1678
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1679

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

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

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

1696
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
27,512,042✔
1697
  int32_t code = 0;
27,512,042✔
1698

1699
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
27,512,042✔
1700
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
27,512,067✔
1701

1702
  if (pOper->fpSet.resetStateFn) {
27,511,857✔
1703
    code = pOper->fpSet.resetStateFn(pOper);
27,512,530✔
1704
  }
1705
  pOper->status = OP_NOT_OPENED;
27,510,297✔
1706
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
47,117,099✔
1707
    code = clearStatesForOperator(pOper->pDownstream[i]);
19,602,263✔
1708
  }
1709
  return code;
27,515,050✔
1710
}
1711

1712
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,910,894✔
1713
  int32_t        code = 0;
7,910,894✔
1714
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,910,894✔
1715
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,910,894✔
1716
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,910,894✔
1717
  code = clearStatesForOperator(pOper);
7,910,894✔
1718
  return code;
7,910,892✔
1719
}
1720

1721
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,819,509✔
1722
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,819,509✔
1723
  int64_t        threadId = taosGetSelfPthreadId();
10,819,509✔
1724
  int64_t        curOwner = 0;
10,819,479✔
1725

1726
  *ppRes = NULL;
10,819,479✔
1727

1728
  // todo extract method
1729
  taosRLockLatch(&pTaskInfo->lock);
10,819,479✔
1730
  bool isKilled = isTaskKilled(pTaskInfo);
10,819,726✔
1731
  if (isKilled) {
10,819,726✔
1732
    // clearStreamBlock(pTaskInfo->pRoot);
1733
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1734

1735
    taosRUnLockLatch(&pTaskInfo->lock);
×
1736
    return pTaskInfo->code;
×
1737
  }
1738

1739
  if (pTaskInfo->owner != 0) {
10,819,726✔
1740
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1741
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1742

1743
    taosRUnLockLatch(&pTaskInfo->lock);
×
1744
    return pTaskInfo->code;
×
1745
  }
1746

1747
  pTaskInfo->owner = threadId;
10,819,509✔
1748
  taosRUnLockLatch(&pTaskInfo->lock);
10,819,726✔
1749

1750
  if (pTaskInfo->cost.start == 0) {
10,819,726✔
1751
    pTaskInfo->cost.start = taosGetTimestampUs();
218,249✔
1752
  }
1753

1754
  // error occurs, record the error code and return to client
1755
  int32_t ret = setjmp(pTaskInfo->env);
10,819,726✔
1756
  if (ret != TSDB_CODE_SUCCESS) {
12,606,751✔
1757
    pTaskInfo->code = ret;
1,788,760✔
1758
    (void)cleanUpUdfs();
1,788,760✔
1759
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,788,760✔
1760
    atomic_store_64(&pTaskInfo->owner, 0);
1,788,760✔
1761
    return pTaskInfo->code;
1,788,760✔
1762
  }
1763

1764
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,817,991✔
1765

1766
  int64_t st = taosGetTimestampUs();
10,819,726✔
1767

1768
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,819,726✔
1769
  if (code) {
9,030,937✔
1770
    pTaskInfo->code = code;
×
1771
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1772
  } else {
1773
    *finished = *ppRes == NULL;
9,030,937✔
1774
    code = blockDataCheck(*ppRes);
9,030,937✔
1775
  }
1776
  if (code) {
9,030,724✔
1777
    pTaskInfo->code = code;
×
1778
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1779
  }
1780

1781
  uint64_t el = (taosGetTimestampUs() - st);
9,030,043✔
1782

1783
  pTaskInfo->cost.elapsedTime += el;
9,030,043✔
1784
  if (NULL == *ppRes) {
9,030,290✔
1785
    *useconds = pTaskInfo->cost.elapsedTime;
5,594,029✔
1786
  }
1787

1788
  (void)cleanUpUdfs();
9,030,290✔
1789

1790
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
9,030,966✔
1791
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,030,966✔
1792

1793
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,030,966✔
1794
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1795

1796
  atomic_store_64(&pTaskInfo->owner, 0);
9,030,966✔
1797
  return pTaskInfo->code;
9,030,966✔
1798
}
1799

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

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

1820
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
232,184✔
1821
  SReadHandle    pHandle = {.vnode = pVnode};
232,184✔
1822
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
232,184✔
1823

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

1832
end:
232,184✔
1833
  return 0;
232,184✔
1834
}
1835

1836
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
37,946✔
1837
  bool   listAdded = false;
37,946✔
1838
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
37,946✔
1839
  if (code == 0 && !listAdded) {
37,946✔
1840
    int32_t numOfTables = taosArrayGetSize(pUidList);
31,684✔
1841
    for (int i = 0; i < numOfTables; i++) {
93,158✔
1842
      void* tmp = taosArrayGet(pUidList, i);
61,474✔
1843
      if (tmp == NULL) {
61,474✔
1844
        return terrno;
×
1845
      }
1846
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
61,474✔
1847

1848
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
61,474✔
1849
      if (p == NULL) {
61,474✔
1850
        return terrno;
×
1851
      }
1852
    }
1853
  }
1854
  return code;
37,946✔
1855
}
1856

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

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

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

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

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

1928
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
232,184✔
1929
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
232,184✔
1930
  STableListInfo* pList = pTableListInfo;
232,184✔
1931
  return pList->pTableList;
232,184✔
1932
}
1933

1934
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,217,433✔
1935

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

1941
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
584,370✔
1942
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
584,370✔
1943
}
1944

1945
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
644,532✔
1946
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1947
  int32_t      code = 0;
644,532✔
1948
  SNode*       pNode = 0;
644,532✔
1949
  SNodeList*   pList = NULL;
644,315✔
1950
  SExprInfo*   pExprInfo = NULL;
644,315✔
1951
  int32_t      numOfExprs = 1;
644,317✔
1952
  int32_t*     offset = 0;
644,100✔
1953
  STargetNode* pTargetNode = NULL;
644,100✔
1954
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
644,532✔
1955
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
644,534✔
1956

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

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

2009
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,160,314✔
2010
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,160,314✔
2011
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,160,314✔
2012
  int32_t        code = 0;
2,160,314✔
2013
  SNode*         pNode = NULL;
2,160,314✔
2014
  if (!pForceOutputCols) return 0;
2,160,314✔
2015
  if (!*pRes) {
59,947✔
2016
    code = createDataBlock(pRes);
59,947✔
2017
  }
2018

2019
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
59,947✔
2020
    int32_t idx = 0;
59,947✔
2021
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
239,601✔
2022
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
179,654✔
2023
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
179,654✔
2024
      colInfo.info.precision = pCol->type.precision;
179,654✔
2025
      colInfo.info.scale = pCol->type.scale;
179,654✔
2026
      code = blockDataAppendColInfo(*pRes, &colInfo);
179,654✔
2027
      if (code != 0) break;
179,654✔
2028
    }
2029
  }
2030

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

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

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

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

2149
    memcpy(tbname, pVal, len);
224,882✔
2150
    tbname[len] = '\0';  // ensure null terminated
224,882✔
2151
    if (NULL != strchr(tbname, '.')) {
225,102✔
2152
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2153
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2154
    }
2155
  }
2156
  // TODO free dst
2157
  sclFreeParam(&dst);
227,322✔
2158
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
227,057✔
2159
  return code;
227,057✔
2160
}
2161

2162
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
211,810✔
2163
  if (pParam) {
211,810✔
2164
    if (pParam->tbname) {
211,810✔
2165
      taosMemFree(pParam->tbname);
211,810✔
2166
      pParam->tbname = NULL;
211,810✔
2167
    }
2168
    if (pParam->stbname) {
211,810✔
2169
      taosMemFree(pParam->stbname);
211,810✔
2170
      pParam->stbname = NULL;
211,810✔
2171
    }
2172
    if (pParam->dbFName) {
211,810✔
2173
      taosMemFree(pParam->dbFName);
211,810✔
2174
      pParam->dbFName = NULL;
211,810✔
2175
    }
2176
    if (pParam->pFields) {
211,810✔
2177
      taosArrayDestroy(pParam->pFields);
211,810✔
2178
      pParam->pFields = NULL;
211,810✔
2179
    }
2180
    if (pParam->pTagFields) {
211,810✔
2181
      taosArrayDestroy(pParam->pTagFields);
133,007✔
2182
      pParam->pTagFields = NULL;
133,007✔
2183
    }
2184
    if (pParam->colCids) {
211,810✔
2185
      taosArrayDestroy(pParam->colCids);
1,738✔
2186
      pParam->colCids = NULL;
1,738✔
2187
    }
2188
    if (pParam->tagCids) {
211,810✔
2189
      taosArrayDestroy(pParam->tagCids);
1,115✔
2190
      pParam->tagCids = NULL;
1,115✔
2191
    }
2192
    taosMemFree(pParam);
211,810✔
2193
  }
2194
}
211,810✔
2195

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

2204
  (*ppDst)->suid = pSrc->suid;
211,810✔
2205
  (*ppDst)->sver = pSrc->sver;
211,810✔
2206
  (*ppDst)->tbType = pSrc->tbType;
211,810✔
2207
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
211,600✔
2208
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
211,810✔
2209

2210
  if (pSrc->stbname) {
211,600✔
2211
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
211,810✔
2212
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
211,810✔
2213
  }
2214

2215
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
211,593✔
2216
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
211,593✔
2217

2218
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
211,593✔
2219

2220
  if (pSrc->pFields && pSrc->pFields->size > 0) {
211,810✔
2221
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
211,600✔
2222
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
211,810✔
2223
  } else {
2224
    (*ppDst)->pFields = NULL;
210✔
2225
  }
2226
  
2227
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
211,810✔
2228
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
132,797✔
2229
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
133,007✔
2230
  } else {
2231
    (*ppDst)->pTagFields = NULL;
79,013✔
2232
  }
2233

2234
  if (pSrc->colCids && pSrc->colCids->size > 0) {
211,600✔
2235
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,738✔
2236
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,738✔
2237
  } else {
2238
    (*ppDst)->colCids = NULL;
209,862✔
2239
  }
2240

2241
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
211,600✔
2242
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,115✔
2243
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,115✔
2244
  } else {
2245
    (*ppDst)->tagCids = NULL;
210,695✔
2246
  }
2247

2248
_exit:
211,810✔
2249

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

2261
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
247✔
2262
  return doDropStreamTable(pMsgCb, pOutput, pReq);
247✔
2263
}
2264

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

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

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