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

taosdata / TDengine / #4872

04 Dec 2025 01:55AM UTC coverage: 64.678% (+0.02%) from 64.654%
#4872

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

880 of 2219 new or added lines in 36 files covered. (39.66%)

6146 existing lines in 122 files now uncovered.

159679 of 246882 relevant lines covered (64.68%)

110947965.82 hits per line

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

68.76
/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 gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
689,010✔
44
  gExecInfo.dnode = pDnode;
689,010✔
45
  gExecInfo.getMnode = getMnode;
689,010✔
46
  gExecInfo.getDnodeId = getDnodeId;
689,010✔
47
  return;
689,010✔
48
}
49

50
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
64,791✔
51
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
64,791✔
52
    qError("gExecInfo is not initialized");
×
53
    return TSDB_CODE_APP_ERROR;
×
54
  }
55
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
64,791✔
56
  return TSDB_CODE_SUCCESS;
64,791✔
57
}
58

59
static void cleanupRefPool() {
642,958✔
60
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
642,958✔
61
  taosCloseRef(ref);
642,958✔
62
}
642,958✔
63

64
static void initRefPool() {
642,958✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
642,958✔
66
  (void)atexit(cleanupRefPool);
642,958✔
67
}
642,958✔
68

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

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

87
    SStreamScanInfo* pInfo = pOperator->info;
×
88

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

122
    return TSDB_CODE_SUCCESS;
×
123
  }
124

125
_end:
×
126
  if (code != TSDB_CODE_SUCCESS) {
×
127
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
128
  }
129
  return code;
×
130
}
131

132
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
133
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
134
    if (pOperator->numOfDownstream == 0) {
×
135
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
136
      return TSDB_CODE_APP_ERROR;
×
137
    }
138

139
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
140
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
141
      return TSDB_CODE_APP_ERROR;
×
142
    }
143

144
    pOperator->status = OP_NOT_OPENED;
×
145
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
146
  }
147
  return 0;
×
148
}
149

150
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
15,657,141✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,657,141✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,657,705✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,515,906✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,515,906✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,515,906✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,515,714✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
55,670✔
158
        if (code) {
55,670✔
159
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
160
          return code;
×
161
        }
162
      }
163
    }
164
  } else {
165
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
11,141,173✔
166
  }
167

168
  return 0;
10,389,369✔
169
}
170

171
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
10,390,390✔
172
  SExecTaskInfo* pTaskInfo = tinfo;
10,390,390✔
173
  pTaskInfo->id.queryId = queryId;
10,390,390✔
174
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
10,390,390✔
175

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,390,160✔
178
}
179

180
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
181
  SExecTaskInfo* pTaskInfo = tinfo;
×
182
  return pTaskInfo->status == OP_EXEC_DONE;
×
183
}
184

185
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
186
  if (tinfo == NULL) {
×
187
    return TSDB_CODE_APP_ERROR;
×
188
  }
189

190
  if (pBlocks == NULL || numOfBlocks == 0) {
×
191
    return TSDB_CODE_SUCCESS;
×
192
  }
193

194
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
195

196
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
197
  if (code != TSDB_CODE_SUCCESS) {
×
198
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
199
  } else {
200
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
201
  }
202

203
  return code;
×
204
}
205

206
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
141,049✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
141,049✔
209
    SExecTaskInfo* pTaskInfo = NULL;
22,984✔
210

211
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
22,984✔
212
    if (NULL == pTaskInfo || code != 0) {
22,984✔
213
      return NULL;
×
214
    }
215

216
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
22,984✔
217
    if (NULL == pTaskInfo->pRoot || code != 0) {
22,984✔
218
      taosMemoryFree(pTaskInfo);
×
219
      return NULL;
×
220
    }
221

222
    pTaskInfo->storageAPI = pReaderHandle->api;
22,984✔
223
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
22,984✔
224
    return pTaskInfo;
22,984✔
225
  }
226

227
  SSubplan* pPlan = NULL;
118,065✔
228
  int32_t   code = qStringToSubplan(msg, &pPlan);
118,499✔
229
  if (code != TSDB_CODE_SUCCESS) {
118,474✔
230
    terrno = code;
×
231
    return NULL;
×
232
  }
233

234
  qTaskInfo_t pTaskInfo = NULL;
118,474✔
235
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE);
118,474✔
236
  if (code != TSDB_CODE_SUCCESS) {
118,563✔
237
    qDestroyTask(pTaskInfo);
×
238
    terrno = code;
×
239
    return NULL;
×
240
  }
241

242
  // extract the number of output columns
243
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
118,563✔
244
  *numOfCols = 0;
118,563✔
245

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,390,585✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,272,022✔
249
    if (pSlotDesc->output) {
1,272,022✔
250
      ++(*numOfCols);
1,271,928✔
251
    }
252
  }
253

254
  return pTaskInfo;
118,563✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
761,080✔
258
  if (streamInserterParam == NULL) {
761,080✔
259
    return TSDB_CODE_SUCCESS;
441,736✔
260
  }
261

262
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
319,344✔
263
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
264
    return TSDB_CODE_INVALID_PARA;
×
265
  }
266

267
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
319,344✔
268
    stError("insertParam: invalid db/table name");
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

272
  if (streamInserterParam->suid <= 0 &&
319,344✔
273
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
112,971✔
274
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
275
    return TSDB_CODE_INVALID_PARA;
×
276
  }
277

278
  return TSDB_CODE_SUCCESS;
319,344✔
279
}
280

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

301
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
761,080✔
302
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
761,080✔
303
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
402✔
304
    goto _error;
402✔
305
  }
306

307
  if (streamInserterParam) {
760,678✔
308
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
319,344✔
309
    void*           pSinkManager = NULL;
319,344✔
310
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
319,344✔
311
    if (code != TSDB_CODE_SUCCESS) {
319,344✔
312
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
313
      goto _error;
×
314
    }
315

316
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
319,344✔
317
    if (NULL == pInserterParam) {
319,344✔
318
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
319
      code = terrno;
×
320
      goto _error;
×
321
    }
322
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
319,344✔
323
    TSDB_CHECK_CODE(code, lino, _error);
319,344✔
324
    
325
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
319,344✔
326
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
319,344✔
327

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
319,344✔
329
    if (code) {
319,344✔
330
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
331
    }
332
  }
333
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
760,281✔
334
         tstrerror(code));
335

336
_error:
138,597✔
337

338
  if (code != TSDB_CODE_SUCCESS) {
761,080✔
339
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
402✔
340
    if (pInserterParam != NULL) {
402✔
341
      taosMemoryFree(pInserterParam);
×
342
    }
343
  }
344
  return code;
761,080✔
345
}
346

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,119,224✔
348
  if (pInfo == NULL) {
5,119,224✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,119,224✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,119,224✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,119,224✔
354
    return false;
5,226✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,113,998✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
4,857,123✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
9,971,121✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

362
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
4,580,058✔
363
  if (pHandle == NULL || pScanBaseInfo == NULL) {
4,580,058✔
364
    return;
×
365
  }
366

367
  pScanBaseInfo->readHandle.uid = pHandle->uid;
4,580,058✔
368
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
4,580,058✔
369
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
4,580,058✔
370
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
4,580,058✔
371
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
4,580,058✔
372
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
4,580,058✔
373
}
374

375
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
5,113,998✔
376
  if (pInfo == NULL) {
5,113,998✔
377
    return TSDB_CODE_INVALID_PARA;
×
378
  }
379
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,113,998✔
380
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,113,998✔
381

382
  void*           info = pOperator->info;
5,113,998✔
383
  STableScanBase* pScanBaseInfo = NULL;
5,113,998✔
384

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,113,998✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
256,875✔
387
    setReadHandle(handle, pScanBaseInfo);
256,875✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
4,857,123✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,323,183✔
390
    setReadHandle(handle, pScanBaseInfo);
4,323,183✔
391
  }
392

393
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
5,113,998✔
394
  handle->winRange.ekey);
395
  return pOperator->fpSet.resetStateFn(pOperator);
5,113,998✔
396
}
397

398
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
760,724✔
399
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
400
  if (msg == NULL) {
760,724✔
401
    return TSDB_CODE_INVALID_PARA;
×
402
  }
403

404
  *pTaskInfo = NULL;
760,724✔
405

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

421
  return code;
760,678✔
422
}
423

424
typedef struct {
425
  tb_uid_t tableUid;
426
  tb_uid_t childUid;
427
  int8_t   check;
428
} STqPair;
429

430
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
66,886✔
431
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
432
  int32_t code = TSDB_CODE_SUCCESS;
66,886✔
433
  int32_t lino = 0;
66,886✔
434
  int8_t  locked = 0;
66,886✔
435
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
66,886✔
436

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
66,886✔
438

439
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
66,886✔
440
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
66,886✔
441

442
  int32_t numOfUids = taosArrayGetSize(tableIdList);
66,886✔
443
  if (numOfUids == 0) {
66,886✔
444
    (*ppArrayRes) = qa;
×
445
    goto _error;
×
446
  }
447

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
66,886✔
449

450
  uint64_t suid = 0;
66,886✔
451
  uint64_t uid = 0;
66,886✔
452
  int32_t  type = 0;
66,886✔
453
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
66,886✔
454

455
  // let's discard the tables those are not created according to the queried super table.
456
  SMetaReader mr = {0};
66,886✔
457
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
66,886✔
458

459
  locked = 1;
66,886✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
134,753✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
67,867✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
67,867✔
463

464
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
67,867✔
465
    if (code != TSDB_CODE_SUCCESS) {
67,867✔
466
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
467
      continue;
×
468
    }
469

470
    tDecoderClear(&mr.coder);
67,867✔
471

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

489
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
67,867✔
490
    if (pScanInfo->pTagCond != NULL) {
67,867✔
491
      // tb_uid_t id = mr.me.uid;
492
      item.check = 1;
59,276✔
493
    }
494
    if (taosArrayPush(tUid, &item) == NULL) {
67,867✔
495
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
496
    }
497
  }
498

499
  pAPI->metaReaderFn.clearReader(&mr);
66,886✔
500
  locked = 0;
66,886✔
501

502
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
134,753✔
503
    bool     qualified = false;
67,867✔
504
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
67,867✔
505
    if (t == NULL) {
67,867✔
506
      continue;
×
507
    }
508

509
    if (t->check == 1) {
67,867✔
510
      STableKeyInfo info = {.groupId = 0, .uid = t->childUid};
59,276✔
511
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
59,276✔
512
      if (code != TSDB_CODE_SUCCESS) {
59,276✔
513
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
514
        continue;
×
515
      }
516

517
      if (!qualified) {
59,276✔
518
        continue;
29,680✔
519
      }
520
    }
521

522
    void* tmp = taosArrayPush(qa, &t->tableUid);
38,187✔
523
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
38,187✔
524
  }
525

526
  // handle multiple partition
527

528
_end:
66,886✔
529

530
  if (locked) {
66,886✔
531
    pAPI->metaReaderFn.clearReader(&mr);
×
532
  }
533
  (*ppArrayRes) = qa;
66,886✔
534
_error:
66,886✔
535

536
  taosArrayDestroy(tUid);
66,886✔
537
  if (code != TSDB_CODE_SUCCESS) {
66,886✔
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
66,886✔
541
}
542

543
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
67,089✔
544
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
67,089✔
545
  const char*    id = GET_TASKID(pTaskInfo);
67,089✔
546
  int32_t        code = 0;
67,089✔
547

548
  if (isAdd) {
67,089✔
549
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
66,886✔
550
  }
551

552
  // traverse to the stream scanner node to add this table id
553
  SOperatorInfo* pInfo = NULL;
67,089✔
554
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
67,089✔
555
  if (code != 0 || pInfo == NULL) {
67,089✔
556
    return code;
×
557
  }
558

559
  SStreamScanInfo* pScanInfo = pInfo->info;
67,089✔
560
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
67,089✔
561
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
135,159✔
562
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
68,070✔
563
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
68,070✔
564
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
68,070✔
565
    }
566
  }
567

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

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

591
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
66,886✔
592
    taosWLockLatch(&pTaskInfo->lock);
66,886✔
593

594
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
105,073✔
595
      uint64_t* uid = taosArrayGet(qa, i);
38,187✔
596
      if (!uid) {
38,187✔
597
        taosMemoryFree(keyBuf);
×
598
        taosArrayDestroy(qa);
×
599
        taosWUnLockLatch(&pTaskInfo->lock);
×
600
        return terrno;
×
601
      }
602
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
38,187✔
603

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

619
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
38,187✔
620
      if (code != TSDB_CODE_SUCCESS) {
38,187✔
621
        taosMemoryFree(keyBuf);
×
622
        taosArrayDestroy(qa);
×
623
        taosWUnLockLatch(&pTaskInfo->lock);
×
624
        return code;
×
625
      }
626
    }
627

628
    taosWUnLockLatch(&pTaskInfo->lock);
66,886✔
629
    if (keyBuf != NULL) {
66,886✔
630
      taosMemoryFree(keyBuf);
×
631
    }
632

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

641
  return code;
67,089✔
642
}
643

644
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
269,395,315✔
645
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
646
                                    int32_t idx, bool* tbGet) {
647
  *tbGet = false;
269,395,315✔
648

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
269,401,505✔
650
    return TSDB_CODE_INVALID_PARA;
7,196✔
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
269,396,392✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
269,396,392✔
655
    return TSDB_CODE_SUCCESS;
154,657,666✔
656
  }
657

658
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
114,742,320✔
659
  if (!pSchemaInfo) {
114,730,166✔
660
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
661
    return terrno;
×
662
  }
663

664
  *sversion = pSchemaInfo->sw->version;
114,730,166✔
665
  *tversion = pSchemaInfo->tversion;
114,748,718✔
666
  *rversion = pSchemaInfo->rversion;
114,735,155✔
667
  if (pSchemaInfo->dbname) {
114,751,577✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
114,744,509✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
114,760,614✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
114,750,593✔
674
  } else {
675
    tableName[0] = 0;
6✔
676
  }
677

678
  *tbGet = true;
114,760,224✔
679

680
  return TSDB_CODE_SUCCESS;
114,749,880✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
154,654,007✔
684

685
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
686
  if (NULL == pParam) {
×
687
    return;
×
688
  }
689
  freeOperatorParam(pParam, OP_GET_PARAM);
×
690
}
691

692
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
5,236,535✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
5,236,535✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
5,236,535✔
695
}
5,236,535✔
696

697
int32_t qExecutorInit(void) {
4,758,095✔
698
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,758,095✔
699
  return TSDB_CODE_SUCCESS;
4,758,835✔
700
}
701

702
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
156,552,952✔
703
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
704
                        EOPTR_EXEC_MODEL model) {
705
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
156,552,952✔
706
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
156,552,952✔
707

708
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
156,552,000✔
709

710
  readHandle->uid = 0;
156,569,738✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
156,577,376✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
156,516,384✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
82,842✔
714
    goto _error;
27,499✔
715
  }
716

717
  if (handle) {
156,447,411✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
156,359,680✔
719
    void*           pSinkManager = NULL;
156,383,219✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
156,264,414✔
721
    if (code != TSDB_CODE_SUCCESS) {
156,375,278✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
156,375,278✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
156,372,854✔
728
    if (code != TSDB_CODE_SUCCESS) {
156,279,920✔
729
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
730
      taosMemoryFree(pSinkManager);
×
731
      goto _error;
×
732
    }
733

734
    SDataSinkNode* pSink = NULL;
156,279,920✔
735
    if (readHandle->localExec) {
156,338,463✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
76,347✔
737
      if (code != TSDB_CODE_SUCCESS) {
76,347✔
738
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
739
               (*pTask)->id.str);
740
        taosMemoryFree(pSinkManager);
×
741
        goto _error;
×
742
      }
743
    }
744

745
    // pSinkParam has been freed during create sinker.
746
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
156,320,525✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
156,383,635✔
748
    if (code) {
156,286,077✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
633✔
750
    }
751
  }
752

753
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
156,478,306✔
754
         tstrerror(code));
755

756
_error:
10,324,463✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
156,570,572✔
759
}
760

761
static void freeBlock(void* param) {
×
762
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
763
  blockDataDestroy(pBlock);
×
764
}
×
765

766
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
177,978,513✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
177,978,513✔
769
  int32_t        lino = 0;
177,978,513✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
177,978,513✔
771
  int64_t        threadId = taosGetSelfPthreadId();
177,978,513✔
772

773
  if (pLocal) {
177,972,954✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
172,384,714✔
775
  }
776

777
  taosArrayClear(pResList);
177,957,189✔
778

779
  int64_t curOwner = 0;
177,965,103✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
177,965,103✔
781
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
782
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
783
    return pTaskInfo->code;
×
784
  }
785

786
  if (pTaskInfo->cost.start == 0) {
177,964,088✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
154,945,992✔
788
  }
789

790
  if (isTaskKilled(pTaskInfo)) {
177,972,230✔
UNCOV
791
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
792
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
793
    return pTaskInfo->code;
×
794
  }
795

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
177,974,274✔
798
  if (ret != TSDB_CODE_SUCCESS) {
178,189,261✔
799
    pTaskInfo->code = ret;
241,020✔
800
    (void)cleanUpUdfs();
241,020✔
801

802
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
241,020✔
803
    atomic_store_64(&pTaskInfo->owner, 0);
241,020✔
804

805
    return pTaskInfo->code;
241,020✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
177,948,241✔
809

810
  int32_t      current = 0;
177,950,236✔
811
  SSDataBlock* pRes = NULL;
177,950,236✔
812
  int64_t      st = taosGetTimestampUs();
177,977,759✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
177,977,759✔
815
    pTaskInfo->paramSet = true;
5,236,535✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
5,236,535✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
172,737,427✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
177,741,270✔
822
  code = blockDataCheck(pRes);
177,741,270✔
823
  QUERY_CHECK_CODE(code, lino, _end);
177,739,359✔
824

825
  if (pRes == NULL) {
177,739,359✔
826
    st = taosGetTimestampUs();
30,833,958✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
177,741,099✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
177,739,872✔
831
    rowsThreshold = 4096;
176,986,204✔
832
  }
833

834
  int32_t blockIndex = 0;
177,739,862✔
835
  while (pRes != NULL) {
568,369,443✔
836
    SSDataBlock* p = NULL;
408,140,945✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
408,133,864✔
838
      SSDataBlock* p1 = NULL;
302,673,829✔
839
      code = createOneDataBlock(pRes, true, &p1);
302,676,271✔
840
      QUERY_CHECK_CODE(code, lino, _end);
302,664,961✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
302,664,961✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
302,673,870✔
844
      p = p1;
302,673,870✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
105,469,570✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
105,470,354✔
848

849
      p = *(SSDataBlock**)tmp;
105,470,354✔
850
      code = copyDataBlock(p, pRes);
105,470,354✔
851
      QUERY_CHECK_CODE(code, lino, _end);
105,468,606✔
852
    }
853

854
    blockIndex += 1;
408,143,738✔
855

856
    current += p->info.rows;
408,143,738✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
408,141,795✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
408,145,215✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
408,145,215✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
408,145,215✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
390,632,382✔
867
    QUERY_CHECK_CODE(code, lino, _end);
390,600,161✔
868
    code = blockDataCheck(pRes);
390,600,161✔
869
    QUERY_CHECK_CODE(code, lino, _end);
390,632,251✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
177,741,331✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
751,803✔
874
  }
875

876
  *hasMore = (pRes != NULL);
177,743,625✔
877
  uint64_t el = (taosGetTimestampUs() - st);
177,735,964✔
878

879
  pTaskInfo->cost.elapsedTime += el;
177,735,964✔
880
  if (NULL == pRes) {
177,739,658✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
160,226,825✔
882
  }
883

884
_end:
177,599,596✔
885
  (void)cleanUpUdfs();
177,740,438✔
886

887
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
177,746,608✔
888
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
177,746,608✔
889
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
890

891
  atomic_store_64(&pTaskInfo->owner, 0);
177,746,774✔
892
  if (code) {
177,746,410✔
893
    pTaskInfo->code = code;
×
894
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
895
  }
896

897
  return pTaskInfo->code;
177,746,410✔
898
}
899

900
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
901
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
902
  SArray*        pList = pTaskInfo->pResultBlockList;
×
903
  size_t         num = taosArrayGetSize(pList);
×
904
  for (int32_t i = 0; i < num; ++i) {
×
905
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
906
    if (p) {
×
907
      blockDataDestroy(*p);
×
908
    }
909
  }
910

911
  taosArrayClear(pTaskInfo->pResultBlockList);
×
912
}
×
913

914
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
39,842,447✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,842,447✔
916
  int64_t        threadId = taosGetSelfPthreadId();
39,842,447✔
917
  int64_t        curOwner = 0;
39,843,517✔
918

919
  *pRes = NULL;
39,843,517✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
39,843,303✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
39,843,874✔
924
  if (isKilled) {
39,843,945✔
925
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
926

927
    taosRUnLockLatch(&pTaskInfo->lock);
×
928
    return pTaskInfo->code;
×
929
  }
930

931
  if (pTaskInfo->owner != 0) {
39,843,945✔
932
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
933
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
934

935
    taosRUnLockLatch(&pTaskInfo->lock);
×
936
    return pTaskInfo->code;
×
937
  }
938

939
  pTaskInfo->owner = threadId;
39,843,624✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
39,843,945✔
941

942
  if (pTaskInfo->cost.start == 0) {
39,843,838✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
94,334✔
944
  }
945

946
  // error occurs, record the error code and return to client
947
  int32_t ret = setjmp(pTaskInfo->env);
39,843,838✔
948
  if (ret != TSDB_CODE_SUCCESS) {
39,843,517✔
949
    pTaskInfo->code = ret;
×
950
    (void)cleanUpUdfs();
×
951
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
952
    atomic_store_64(&pTaskInfo->owner, 0);
×
953
    return pTaskInfo->code;
×
954
  }
955

956
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
39,843,517✔
957

958
  int64_t st = taosGetTimestampUs();
39,843,303✔
959
  int32_t code = TSDB_CODE_SUCCESS;
39,843,303✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
39,843,303✔
961
    pTaskInfo->paramSet = true;
×
962
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
963
  } else {
964
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
39,842,875✔
965
  }
966
  if (code) {
39,832,142✔
967
    pTaskInfo->code = code;
×
968
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
969
  }
970

971
  code = blockDataCheck(*pRes);
39,832,142✔
972
  if (code) {
39,843,562✔
973
    pTaskInfo->code = code;
×
974
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
975
  }
976

977
  uint64_t el = (taosGetTimestampUs() - st);
39,830,915✔
978

979
  pTaskInfo->cost.elapsedTime += el;
39,830,915✔
980
  if (NULL == *pRes) {
39,840,984✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
4,192,416✔
982
  }
983

984
  (void)cleanUpUdfs();
39,841,789✔
985

986
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
39,843,838✔
987
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
39,843,945✔
988

989
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
39,843,945✔
990
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
991

992
  atomic_store_64(&pTaskInfo->owner, 0);
39,842,246✔
993
  return pTaskInfo->code;
39,843,838✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
51,186,353✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
51,186,353✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
51,186,611✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
51,186,611✔
1000

1001
  if (!tmp) {
51,186,353✔
1002
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1003
    return terrno;
×
1004
  }
1005
  return TSDB_CODE_SUCCESS;
51,186,353✔
1006
}
1007

1008
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1009
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1010
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1011

1012
  if (key->refId < pInfo->refId) {
×
1013
    return -1;
×
1014
  } else if (key->refId > pInfo->refId) {
×
1015
    return 1;
×
1016
  }
1017

1018
  return 0;
×
1019
}
1020

1021
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1022
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1023
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1024
  if (idx >= 0) {
×
1025
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1026
  }
1027
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1028
}
×
1029

1030
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
16,648✔
1031
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
16,648✔
1032

1033
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
16,648✔
1034
  for (int32_t i = 0; i < num; ++i) {
20,685✔
1035
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
4,037✔
1036
    if (!pStop) {
4,037✔
1037
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1038
      continue;
×
1039
    }
1040
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
4,037✔
1041
    if (pExchangeInfo) {
4,037✔
1042
      int32_t code = tsem_post(&pExchangeInfo->ready);
4,037✔
1043
      if (code != TSDB_CODE_SUCCESS) {
4,037✔
1044
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1045
      } else {
1046
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
4,037✔
1047
      }
1048
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
4,037✔
1049
      if (code != TSDB_CODE_SUCCESS) {
4,037✔
1050
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1051
      }
1052
    }
1053
  }
1054

1055
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
16,648✔
1056
}
16,648✔
1057

1058
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
16,648✔
1059
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
16,648✔
1060
  if (pTaskInfo == NULL) {
16,648✔
1061
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1062
  }
1063

1064
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
16,648✔
1065

1066
  setTaskKilled(pTaskInfo, rspCode);
16,648✔
1067
  qStopTaskOperators(pTaskInfo);
16,648✔
1068

1069
  return TSDB_CODE_SUCCESS;
16,648✔
1070
}
1071

1072
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1073
  int64_t        st = taosGetTimestampMs();
×
1074
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1075
  if (pTaskInfo == NULL) {
×
1076
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1077
  }
1078

1079
  if (waitDuration > 0) {
×
1080
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1081
  } else {
1082
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1083
  }
1084

1085
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1086

1087
  if (waitDuration > 0) {
×
1088
    while (1) {
1089
      taosWLockLatch(&pTaskInfo->lock);
×
1090
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1091
        taosWUnLockLatch(&pTaskInfo->lock);
×
1092

1093
        taosMsleep(200);
×
1094

1095
        int64_t d = taosGetTimestampMs() - st;
×
1096
        if (d >= waitDuration && waitDuration >= 0) {
×
1097
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1098
          return TSDB_CODE_SUCCESS;
×
1099
        }
1100
      } else {  // not running now
1101
        pTaskInfo->code = rspCode;
×
1102
        taosWUnLockLatch(&pTaskInfo->lock);
×
1103
        return TSDB_CODE_SUCCESS;
×
1104
      }
1105
    }
1106
  }
1107

1108
  int64_t et = taosGetTimestampMs() - st;
×
1109
  if (et < waitDuration) {
×
1110
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1111
    return TSDB_CODE_SUCCESS;
×
1112
  }
1113
  return TSDB_CODE_SUCCESS;
×
1114
}
1115

1116
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1117
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1118
  if (NULL == pTaskInfo) {
×
1119
    return false;
×
1120
  }
1121

1122
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1123
}
1124

1125
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
157,334,598✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
157,334,598✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
157,336,379✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
157,334,160✔
1130
  if (pSummary->pRecoder != NULL) {
157,329,965✔
1131
    qDebug(
113,958,236✔
1132
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1133
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1134
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1135
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1136
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1137
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1138
  } else {
1139
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
43,374,645✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
157,332,881✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
164,524,269✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
164,524,269✔
1146
  if (pTaskInfo == NULL) {
164,524,269✔
1147
    return;
7,191,338✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
157,332,931✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
157,334,131✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
157,335,075✔
1157
  doDestroyTask(pTaskInfo);
157,334,070✔
1158
}
1159

1160
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,649,192✔
1161
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,649,192✔
1162
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,649,192✔
1163
}
1164

1165
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
118,563✔
1166
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
118,563✔
1167
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
118,563✔
1168

1169
  while (1) {
115,807✔
1170
    uint16_t type = pOperator->operatorType;
234,370✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
234,370✔
1172
      *scanner = pOperator->info;
118,563✔
1173
      break;
118,563✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
115,807✔
1176
    }
1177
  }
1178
}
118,563✔
1179

1180
void* qExtractReaderFromTmqScanner(void* scanner) {
118,563✔
1181
  SStreamScanInfo* pInfo = scanner;
118,563✔
1182
  return (void*)pInfo->tqReader;
118,563✔
1183
}
1184

1185
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
136,673✔
1186
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
136,673✔
1187
  return pTaskInfo->streamInfo.schema;
136,673✔
1188
}
1189

1190
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
136,673✔
1191
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
136,673✔
1192
  return pTaskInfo->streamInfo.tbName;
136,673✔
1193
}
1194

1195
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
150,144✔
1196
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
150,144✔
1197
  return &pTaskInfo->streamInfo.btMetaRsp;
150,144✔
1198
}
1199

1200
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,614,861✔
1201
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,614,861✔
1202
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,614,861✔
1203
  return 0;
4,614,861✔
1204
  /*if (code != TSDB_CODE_SUCCESS) {
1205
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1206
    pTaskInfo->code = code;
1207
    T_LONG_JMP(pTaskInfo->env, code);
1208
  }*/
1209
}
1210

1211
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
142,433✔
1212
  memset(pCond, 0, sizeof(SQueryTableDataCond));
142,433✔
1213
  pCond->order = TSDB_ORDER_ASC;
142,433✔
1214
  pCond->numOfCols = pMtInfo->schema->nCols;
142,424✔
1215
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
142,541✔
1216
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
142,478✔
1217
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
142,541✔
1218
    taosMemoryFreeClear(pCond->colList);
54✔
UNCOV
1219
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1220
    return terrno;
×
1221
  }
1222

1223
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
142,487✔
1224
  pCond->suid = pMtInfo->suid;
142,487✔
1225
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
142,433✔
1226
  pCond->startVersion = -1;
142,595✔
1227
  pCond->endVersion = sContext->snapVersion;
142,433✔
1228

1229
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
910,270✔
1230
    SColumnInfo* pColInfo = &pCond->colList[i];
767,684✔
1231
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
767,801✔
1232
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
767,801✔
1233
    if (pMtInfo->pExtSchemas != NULL) {
767,801✔
1234
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,486✔
1235
    }
1236
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
767,792✔
1237
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
767,684✔
1238

1239
    pCond->pSlotList[i] = i;
767,792✔
1240
  }
1241

1242
  return TSDB_CODE_SUCCESS;
142,541✔
1243
}
1244

1245
void qStreamSetOpen(qTaskInfo_t tinfo) {
39,559,409✔
1246
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,559,409✔
1247
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
39,559,409✔
1248
  pOperator->status = OP_NOT_OPENED;
39,561,684✔
1249
}
39,561,630✔
1250

1251
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,460,979✔
1252
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,460,979✔
1253
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,460,979✔
1254
}
4,461,693✔
1255

1256
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,668,740✔
1257
  int32_t        code = TSDB_CODE_SUCCESS;
4,668,740✔
1258
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,668,740✔
1259
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,668,740✔
1260

1261
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,668,863✔
1262
  const char*    id = GET_TASKID(pTaskInfo);
4,669,003✔
1263

1264
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,668,285✔
1265
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,398,573✔
1266
    if (pOperator == NULL || code != 0) {
4,397,818✔
1267
      return code;
510✔
1268
    }
1269

1270
    SStreamScanInfo* pInfo = pOperator->info;
4,397,308✔
1271
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,397,368✔
1272
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,398,467✔
1273
    walReaderVerifyOffset(pWalReader, pOffset);
4,398,867✔
1274
  }
1275
  // if pOffset equal to current offset, means continue consume
1276
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,668,397✔
1277
    return 0;
4,256,417✔
1278
  }
1279

1280
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
411,551✔
1281
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
265,572✔
1282
    if (pOperator == NULL || code != 0) {
265,455✔
UNCOV
1283
      return code;
×
1284
    }
1285

1286
    SStreamScanInfo* pInfo = pOperator->info;
265,509✔
1287
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
265,509✔
1288
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
265,509✔
1289
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
265,509✔
1290

1291
    if (pOffset->type == TMQ_OFFSET__LOG) {
265,509✔
1292
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
206,201✔
1293
      pScanBaseInfo->dataReader = NULL;
206,101✔
1294

1295
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
206,101✔
1296
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
206,101✔
1297
      walReaderVerifyOffset(pWalReader, pOffset);
205,991✔
1298
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
206,201✔
1299
      if (code < 0) {
206,201✔
1300
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
6,126✔
1301
        return code;
6,126✔
1302
      }
1303
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
59,254✔
1304
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1305
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1306
      int64_t uid = pOffset->uid;
59,371✔
1307
      int64_t ts = pOffset->ts;
59,308✔
1308
      int32_t index = 0;
59,308✔
1309

1310
      // this value may be changed if new tables are created
1311
      taosRLockLatch(&pTaskInfo->lock);
59,308✔
1312
      int32_t numOfTables = 0;
59,371✔
1313
      code = tableListGetSize(pTableListInfo, &numOfTables);
59,371✔
1314
      if (code != TSDB_CODE_SUCCESS) {
59,307✔
UNCOV
1315
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1316
        taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1317
        return code;
×
1318
      }
1319

1320
      if (uid == 0) {
59,307✔
1321
        if (numOfTables != 0) {
58,354✔
1322
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,196✔
1323
          if (!tmp) {
10,267✔
UNCOV
1324
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1325
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1326
            return terrno;
×
1327
          }
1328
          if (tmp) uid = tmp->uid;
10,267✔
1329
          ts = INT64_MIN;
10,267✔
1330
          pScanInfo->currentTable = 0;
10,267✔
1331
        } else {
1332
          taosRUnLockLatch(&pTaskInfo->lock);
48,158✔
1333
          qError("no table in table list, %s", id);
47,948✔
1334
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
48,087✔
1335
        }
1336
      }
1337
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
11,220✔
1338

1339
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
11,230✔
1340
             pInfo->pTableScanOp->resultInfo.totalRows);
1341
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
11,230✔
1342

1343
      // start from current accessed position
1344
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1345
      // position, let's find it from the beginning.
1346
      index = tableListFind(pTableListInfo, uid, 0);
11,284✔
1347
      taosRUnLockLatch(&pTaskInfo->lock);
11,284✔
1348

1349
      if (index >= 0) {
11,284✔
1350
        pScanInfo->currentTable = index;
11,284✔
1351
      } else {
UNCOV
1352
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1353
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1354
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1355
      }
1356

1357
      STableKeyInfo keyInfo = {.uid = uid};
11,284✔
1358
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
11,284✔
1359

1360
      // let's start from the next ts that returned to consumer.
1361
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
11,284✔
UNCOV
1362
        pScanBaseInfo->cond.twindows.skey = ts;
×
1363
      } else {
1364
        pScanBaseInfo->cond.twindows.skey = ts + 1;
11,284✔
1365
      }
1366
      pScanInfo->scanTimes = 0;
11,284✔
1367

1368
      if (pScanBaseInfo->dataReader == NULL) {
11,284✔
1369
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
19,708✔
1370
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1371
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
9,854✔
1372
        if (code != TSDB_CODE_SUCCESS) {
9,826✔
UNCOV
1373
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
UNCOV
1374
          return code;
×
1375
        }
1376

1377
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
9,826✔
1378
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1379
      } else {
1380
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
1,430✔
1381
        if (code != TSDB_CODE_SUCCESS) {
1,430✔
UNCOV
1382
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1383
          return code;
×
1384
        }
1385

1386
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
1,430✔
1387
        if (code != TSDB_CODE_SUCCESS) {
1,430✔
UNCOV
1388
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1389
          return code;
×
1390
        }
1391
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
1,430✔
1392
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1393
      }
1394

1395
      // restore the key value
1396
      pScanBaseInfo->cond.twindows.skey = oldSkey;
11,284✔
1397
    } else {
UNCOV
1398
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
UNCOV
1399
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1400
    }
1401

1402
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1403
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
145,979✔
1404
      SStreamRawScanInfo* pInfo = pOperator->info;
142,778✔
1405
      SSnapContext*       sContext = pInfo->sContext;
142,778✔
1406
      SOperatorInfo*      p = NULL;
142,778✔
1407

1408
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
142,778✔
1409
      if (code != 0) {
142,778✔
UNCOV
1410
        return code;
×
1411
      }
1412

1413
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
142,778✔
1414

1415
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
142,778✔
UNCOV
1416
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
UNCOV
1417
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1418
      }
1419

1420
      SMetaTableInfo mtInfo = {0};
142,778✔
1421
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
142,778✔
1422
      if (code != 0) {
142,724✔
1423
        destroyMetaTableInfo(&mtInfo);
UNCOV
1424
        return code;
×
1425
      }
1426
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
142,724✔
1427
      pInfo->dataReader = NULL;
142,598✔
1428

1429
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
142,652✔
1430
      tableListClear(pTableListInfo);
142,670✔
1431

1432
      if (mtInfo.uid == 0) {
142,778✔
1433
        destroyMetaTableInfo(&mtInfo);
1434
        goto end;  // no data
183✔
1435
      }
1436

1437
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
142,595✔
1438
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
142,424✔
1439
      if (code != TSDB_CODE_SUCCESS) {
142,487✔
1440
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1441
        destroyMetaTableInfo(&mtInfo);
UNCOV
1442
        return code;
×
1443
      }
1444
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
142,487✔
UNCOV
1445
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1446
      } else {
1447
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
142,541✔
1448
      }
1449

1450
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
142,487✔
1451
      if (code != TSDB_CODE_SUCCESS) {
142,595✔
1452
        destroyMetaTableInfo(&mtInfo);
1453
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1454
        return code;
×
1455
      }
1456

1457
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
142,595✔
1458
      if (!pList) {
142,541✔
UNCOV
1459
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1460
        destroyMetaTableInfo(&mtInfo);
1461
        return code;
×
1462
      }
1463
      int32_t size = 0;
142,541✔
1464
      code = tableListGetSize(pTableListInfo, &size);
142,541✔
1465
      if (code != TSDB_CODE_SUCCESS) {
142,595✔
UNCOV
1466
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1467
        destroyMetaTableInfo(&mtInfo);
UNCOV
1468
        return code;
×
1469
      }
1470

1471
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
285,190✔
1472
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
142,595✔
1473
      if (code != TSDB_CODE_SUCCESS) {
142,595✔
UNCOV
1474
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1475
        destroyMetaTableInfo(&mtInfo);
UNCOV
1476
        return code;
×
1477
      }
1478

1479
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
142,595✔
1480
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
142,532✔
1481
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1482
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
142,532✔
1483
      pTaskInfo->streamInfo.schema = mtInfo.schema;
142,532✔
1484
      taosMemoryFreeClear(mtInfo.pExtSchemas);
142,532✔
1485

1486
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
142,532✔
1487
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,201✔
1488
      SStreamRawScanInfo* pInfo = pOperator->info;
1,073✔
1489
      SSnapContext*       sContext = pInfo->sContext;
1,073✔
1490
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,073✔
1491
      if (code != 0) {
1,073✔
UNCOV
1492
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
UNCOV
1493
        return code;
×
1494
      }
1495
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
1,073✔
1496
             id);
1497
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
2,128✔
1498
      SStreamRawScanInfo* pInfo = pOperator->info;
2,128✔
1499
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,128✔
1500
      pInfo->dataReader = NULL;
2,128✔
1501
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
2,128✔
1502
    }
1503
  }
1504

1505
end:
356,954✔
1506
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
357,279✔
1507
  return 0;
357,284✔
1508
}
1509

1510
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
107,855,347✔
1511
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
107,855,347✔
1512
  if (pMsg->info.ahandle == NULL) {
107,860,632✔
UNCOV
1513
    qError("pMsg->info.ahandle is NULL");
×
UNCOV
1514
    return;
×
1515
  }
1516

1517
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
107,844,119✔
1518
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1519

1520
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
107,848,880✔
1521

1522
  if (pMsg->contLen > 0) {
107,857,344✔
1523
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
107,843,163✔
1524
    if (buf.pData == NULL) {
107,837,261✔
1525
      pMsg->code = terrno;
×
1526
    } else {
1527
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
107,837,261✔
1528
    }
1529
  }
1530

1531
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
107,858,963✔
1532
  rpcFreeCont(pMsg->pCont);
107,865,203✔
1533
  destroySendMsgInfo(pSendInfo);
107,848,034✔
1534
}
1535

UNCOV
1536
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1537
  int32_t        code = TSDB_CODE_SUCCESS;
×
1538
  int32_t        lino = 0;
×
UNCOV
1539
  SExecTaskInfo* pTaskInfo = tinfo;
×
1540
  SArray*        plist = NULL;
×
1541

1542
  code = getTableListInfo(pTaskInfo, &plist);
×
UNCOV
1543
  if (code || plist == NULL) {
×
1544
    return NULL;
×
1545
  }
1546

1547
  // only extract table in the first elements
1548
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1549

UNCOV
1550
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1551
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1552

1553
  int32_t numOfTables = 0;
×
1554
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1555
  QUERY_CHECK_CODE(code, lino, _end);
×
1556

UNCOV
1557
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1558
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
UNCOV
1559
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
UNCOV
1560
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
UNCOV
1561
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1562
  }
1563

UNCOV
1564
  taosArrayDestroy(plist);
×
1565

UNCOV
1566
_end:
×
1567
  if (code != TSDB_CODE_SUCCESS) {
×
1568
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
1569
    T_LONG_JMP(pTaskInfo->env, code);
×
1570
  }
1571
  return pUidList;
×
1572
}
1573

1574
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,387,824✔
1575
  int32_t        code = TSDB_CODE_SUCCESS;
3,387,824✔
1576
  int32_t        lino = 0;
3,387,824✔
1577
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,387,824✔
1578

1579
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,388,423✔
UNCOV
1580
    SStreamScanInfo* pScanInfo = pOperator->info;
×
UNCOV
1581
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1582

1583
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
UNCOV
1584
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1585
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,387,824✔
1586
    STableScanInfo* pScanInfo = pOperator->info;
1,693,912✔
1587

1588
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,693,912✔
1589
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,693,912✔
1590
  } else {
1591
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,693,912✔
1592
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,693,313✔
1593
    }
1594
  }
1595

UNCOV
1596
_end:
×
1597
  if (code != TSDB_CODE_SUCCESS) {
3,387,225✔
1598
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1599
  }
1600
  return code;
3,387,824✔
1601
}
1602

1603
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,693,912✔
1604
  if (pList == NULL) {
1,693,912✔
1605
    return TSDB_CODE_INVALID_PARA;
×
1606
  }
1607

1608
  *pList = NULL;
1,693,912✔
1609
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,693,315✔
1610
  if (pArray == NULL) {
1,693,315✔
1611
    return terrno;
×
1612
  }
1613

1614
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,693,315✔
1615
  if (code == 0) {
1,693,912✔
1616
    *pList = pArray;
1,693,912✔
1617
  } else {
1618
    taosArrayDestroy(pArray);
×
1619
  }
1620
  return code;
1,693,912✔
1621
}
1622

1623
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
UNCOV
1624
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1625
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1626
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1627
  }
UNCOV
1628
  return 0;
×
1629
}
1630

1631
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1632
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1633
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1634
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1635
  }
1636
  return 0;
×
1637
}
1638

1639
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1640
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1641

1642
  int32_t code = pTaskInfo->code;
×
UNCOV
1643
  pTaskInfo->code = 0;
×
UNCOV
1644
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
UNCOV
1645
}
×
1646

UNCOV
1647
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
UNCOV
1648
  int32_t code = 0;
×
UNCOV
1649
  return code;
×
1650
}
1651

UNCOV
1652
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
UNCOV
1653
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1654
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
UNCOV
1655
  return code;
×
1656
}
1657

1658
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
39,258,329✔
1659
  int32_t code = 0;
39,258,329✔
1660

1661
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
39,258,329✔
1662
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
39,255,978✔
1663

1664
  if (pOper->fpSet.resetStateFn) {
39,254,787✔
1665
    code = pOper->fpSet.resetStateFn(pOper);
39,256,344✔
1666
  }
1667
  pOper->status = OP_NOT_OPENED;
39,253,031✔
1668
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
66,826,139✔
1669
    code = clearStatesForOperator(pOper->pDownstream[i]);
27,566,658✔
1670
  }
1671
  return code;
39,262,696✔
1672
}
1673

1674
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
11,690,836✔
1675
  int32_t        code = 0;
11,690,836✔
1676
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,690,836✔
1677
  SOperatorInfo* pOper = pTaskInfo->pRoot;
11,690,836✔
1678
  pTaskInfo->code = TSDB_CODE_SUCCESS;
11,691,233✔
1679
  code = clearStatesForOperator(pOper);
11,691,233✔
1680
  return code;
11,690,462✔
1681
}
1682

1683
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
15,536,212✔
1684
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
15,536,212✔
1685
  int64_t        threadId = taosGetSelfPthreadId();
15,536,212✔
1686
  int64_t        curOwner = 0;
15,536,212✔
1687

1688
  *ppRes = NULL;
15,536,212✔
1689

1690
  // todo extract method
1691
  taosRLockLatch(&pTaskInfo->lock);
15,536,612✔
1692
  bool isKilled = isTaskKilled(pTaskInfo);
15,537,009✔
1693
  if (isKilled) {
15,536,609✔
1694
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1695
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1696

UNCOV
1697
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1698
    return pTaskInfo->code;
×
1699
  }
1700

1701
  if (pTaskInfo->owner != 0) {
15,536,609✔
UNCOV
1702
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1703
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1704

UNCOV
1705
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1706
    return pTaskInfo->code;
×
1707
  }
1708

1709
  pTaskInfo->owner = threadId;
15,537,009✔
1710
  taosRUnLockLatch(&pTaskInfo->lock);
15,536,609✔
1711

1712
  if (pTaskInfo->cost.start == 0) {
15,537,009✔
1713
    pTaskInfo->cost.start = taosGetTimestampUs();
328,060✔
1714
  }
1715

1716
  // error occurs, record the error code and return to client
1717
  int32_t ret = setjmp(pTaskInfo->env);
15,537,009✔
1718
  if (ret != TSDB_CODE_SUCCESS) {
18,120,747✔
1719
    pTaskInfo->code = ret;
2,586,545✔
1720
    (void)cleanUpUdfs();
2,586,545✔
1721
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,586,545✔
1722
    atomic_store_64(&pTaskInfo->owner, 0);
2,586,545✔
1723
    return pTaskInfo->code;
2,586,545✔
1724
  }
1725

1726
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
15,534,202✔
1727

1728
  int64_t st = taosGetTimestampUs();
15,536,609✔
1729

1730
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
15,536,609✔
1731
  if (code) {
12,950,464✔
UNCOV
1732
    pTaskInfo->code = code;
×
UNCOV
1733
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1734
  } else {
1735
    *finished = *ppRes == NULL;
12,950,464✔
1736
    code = blockDataCheck(*ppRes);
12,950,464✔
1737
  }
1738
  if (code) {
12,950,464✔
UNCOV
1739
    pTaskInfo->code = code;
×
UNCOV
1740
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1741
  }
1742

1743
  uint64_t el = (taosGetTimestampUs() - st);
12,950,464✔
1744

1745
  pTaskInfo->cost.elapsedTime += el;
12,950,464✔
1746
  if (NULL == *ppRes) {
12,950,464✔
1747
    *useconds = pTaskInfo->cost.elapsedTime;
8,510,433✔
1748
  }
1749

1750
  (void)cleanUpUdfs();
12,950,464✔
1751

1752
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
12,950,464✔
1753
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
12,950,464✔
1754

1755
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
12,950,464✔
1756
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1757

1758
  atomic_store_64(&pTaskInfo->owner, 0);
12,950,464✔
1759
  return pTaskInfo->code;
12,950,464✔
1760
}
1761

1762
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1763
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1764
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1765
// }
1766

1767
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
319,966✔
1768
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1769
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1770
  int32_t code = 0;                                        
319,966✔
1771
  if (*pTableListInfo != NULL) {
319,966✔
UNCOV
1772
    qDebug("table list already exists, no need to create again");
×
UNCOV
1773
    goto end;
×
1774
  }
1775
  STableListInfo* pList = tableListCreate();
319,966✔
1776
  if (pList == NULL) {
319,966✔
1777
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1778
    code = terrno;
×
UNCOV
1779
    goto end;
×
1780
  }
1781

1782
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
319,966✔
1783
  SReadHandle    pHandle = {.vnode = pVnode};
319,966✔
1784
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
319,966✔
1785

1786
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
319,966✔
1787
  if (code != 0) {
319,966✔
UNCOV
1788
    tableListDestroy(pList);
×
UNCOV
1789
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
UNCOV
1790
    goto end;
×
1791
  }
1792
  *pTableListInfo = pList;
319,966✔
1793

1794
end:
319,966✔
1795
  return 0;
319,966✔
1796
}
1797

1798
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
131,663✔
1799
  bool   listAdded = false;
131,663✔
1800
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
131,663✔
1801
  if (code == 0 && !listAdded) {
131,663✔
1802
    int32_t numOfTables = taosArrayGetSize(pUidList);
122,565✔
1803
    for (int i = 0; i < numOfTables; i++) {
245,130✔
1804
      void* tmp = taosArrayGet(pUidList, i);
122,565✔
1805
      if (tmp == NULL) {
122,565✔
UNCOV
1806
        return terrno;
×
1807
      }
1808
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
122,565✔
1809

1810
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
122,565✔
1811
      if (p == NULL) {
122,565✔
1812
        return terrno;
×
1813
      }
1814
    }
1815
  }
1816
  return code;
131,663✔
1817
}
1818

1819
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
131,663✔
1820
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1821
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1822
  int32_t code = TSDB_CODE_SUCCESS;
131,663✔
1823
  STableListInfo* pList = tableListCreate();
131,663✔
1824
  if (pList == NULL) {
131,663✔
UNCOV
1825
    code = terrno;
×
UNCOV
1826
    goto end;
×
1827
  }
1828
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
131,663✔
1829
  if (uidListCopy == NULL) {
131,663✔
UNCOV
1830
    code = terrno;
×
1831
    goto end;
×
1832
  }
1833
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
131,663✔
1834
  SReadHandle    pHandle = {.vnode = pVnode};
131,663✔
1835

1836
  pList->idInfo.suid = suid;
131,663✔
1837
  pList->idInfo.tableType = TD_SUPER_TABLE;
131,663✔
1838
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
131,663✔
1839
  if (code != TSDB_CODE_SUCCESS) {
131,663✔
1840
    goto end;
×
1841
  }                                              
1842
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
131,663✔
1843
  if (code != TSDB_CODE_SUCCESS) {
131,663✔
UNCOV
1844
    goto end;
×
1845
  }
1846
  *tableList = pList->pTableList;
131,663✔
1847
  pList->pTableList = NULL;
131,663✔
1848

1849
  taosArrayClear(uidList);
131,663✔
1850
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
263,326✔
1851
    void* tmp = taosArrayGet(uidListCopy, i);
131,663✔
1852
    if (tmp == NULL) {
131,663✔
UNCOV
1853
      continue;
×
1854
    }
1855
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
131,663✔
1856
    if (slot == NULL) {
131,663✔
1857
      if (taosArrayPush(uidList, tmp) == NULL) {
6,153✔
UNCOV
1858
        code = terrno;
×
UNCOV
1859
        goto end;
×
1860
      }
1861
    }
1862
  }
1863
end:
131,663✔
1864
  taosArrayDestroy(uidListCopy);
131,663✔
1865
  tableListDestroy(pList);
131,663✔
1866
  return code;
131,663✔
1867
}
1868

1869
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1870
//   int32_t index = -1;
1871
//   (void)taosThreadRwlockRdlock(lock);
1872
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1873
//     index = 0;
1874
//     goto end;
1875
//   }
1876
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1877
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1878

1879
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1880
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1881
//       index = i;
1882
//       goto end;
1883
//     }
1884
//   }
1885
// end:
1886
//   (void)taosThreadRwlockUnlock(lock);
1887
//   return index;
1888
// }
1889

1890
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
319,966✔
1891
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
319,569✔
1892
  STableListInfo* pList = pTableListInfo;
319,569✔
1893
  return pList->pTableList;
319,569✔
1894
}
1895

1896
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,876,061✔
1897

1898
void streamDestroyExecTask(qTaskInfo_t tInfo) {
3,232,390✔
1899
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
3,232,390✔
1900
  qDestroyTask(tInfo);
3,232,390✔
1901
}
3,232,390✔
1902

1903
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
877,517✔
1904
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
877,517✔
1905
}
1906

1907
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
925,699✔
1908
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1909
  int32_t      code = 0;
925,699✔
1910
  SNode*       pNode = 0;
925,699✔
1911
  SNodeList*   pList = NULL;
925,699✔
1912
  SExprInfo*   pExprInfo = NULL;
925,699✔
1913
  int32_t      numOfExprs = 1;
925,699✔
1914
  int32_t*     offset = 0;
925,699✔
1915
  STargetNode* pTargetNode = NULL;
925,699✔
1916
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
925,699✔
1917
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
925,699✔
1918

1919
  if (code == 0) {
925,699✔
1920
    pTargetNode->dataBlockId = 0;
925,699✔
1921
    pTargetNode->pExpr = pNode;
925,699✔
1922
    pTargetNode->slotId = 0;
925,699✔
1923
  }
1924
  if (code == 0) {
925,699✔
1925
    code = nodesMakeList(&pList);
925,699✔
1926
  }
1927
  if (code == 0) {
925,699✔
1928
    code = nodesListAppend(pList, (SNode*)pTargetNode);
925,699✔
1929
  }
1930
  if (code == 0) {
925,699✔
1931
    pNode = NULL;
925,699✔
1932
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
925,699✔
1933
  }
1934

1935
  if (code == 0) {
925,699✔
1936
    const char* pVal = NULL;
925,699✔
1937
    int32_t     len = 0;
925,699✔
1938
    SNode*      pSclNode = NULL;
925,699✔
1939
    switch (pExprInfo->pExpr->nodeType) {
925,699✔
1940
      case QUERY_NODE_FUNCTION:
925,699✔
1941
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
925,699✔
1942
        break;
925,699✔
1943
      case QUERY_NODE_OPERATOR:
×
UNCOV
1944
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
1945
        break;
×
UNCOV
1946
      default:
×
UNCOV
1947
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
1948
        break;
×
1949
    }
1950
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
925,699✔
1951
    SSDataBlock block = {0};
924,511✔
1952
    block.info.rows = 1;
924,511✔
1953
    SSDataBlock* pBlock = &block;
924,511✔
1954
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
925,343✔
1955
    if (tmp == NULL) {
925,343✔
UNCOV
1956
      code = terrno;
×
1957
    }
1958
    if (code == 0) {
925,343✔
1959
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
925,699✔
1960
    }
1961
    taosArrayDestroy(pBlockList);
924,987✔
1962
  }
1963
  nodesDestroyList(pList);
925,699✔
1964
  destroyExprInfo(pExprInfo, numOfExprs);
925,343✔
1965
  taosMemoryFreeClear(pExprInfo);
925,699✔
1966
  return code;
925,343✔
1967
}
1968

1969
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,088,888✔
1970
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,088,888✔
1971
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,088,888✔
1972
  int32_t        code = 0;
4,090,154✔
1973
  SNode*         pNode = NULL;
4,090,154✔
1974
  if (!pForceOutputCols) return 0;
4,090,154✔
1975
  if (!*pRes) {
48,182✔
1976
    code = createDataBlock(pRes);
48,182✔
1977
  }
1978

1979
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
48,182✔
1980
    int32_t idx = 0;
48,182✔
1981
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
192,348✔
1982
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
144,166✔
1983
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
144,166✔
1984
      colInfo.info.precision = pCol->type.precision;
144,166✔
1985
      colInfo.info.scale = pCol->type.scale;
144,166✔
1986
      code = blockDataAppendColInfo(*pRes, &colInfo);
144,166✔
1987
      if (code != 0) break;
144,166✔
1988
    }
1989
  }
1990

1991
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
48,182✔
1992
  if (code != TSDB_CODE_SUCCESS) {
48,182✔
UNCOV
1993
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
1994
    return code;
×
1995
  }
1996

1997
  // loop all exprs for force output, execute all exprs
1998
  int32_t idx = 0;
48,182✔
1999
  int32_t rowIdx = (*pRes)->info.rows;
48,182✔
2000
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
48,182✔
2001
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
48,182✔
2002
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
192,348✔
2003
    SScalarParam   dst = {0};
144,166✔
2004
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
144,166✔
2005
    code = nodesStringToNode(pCol->expr, &pNode);
144,166✔
2006
    if (code != 0) break;
144,166✔
2007
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
144,166✔
2008
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
144,166✔
2009
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
95,984✔
2010
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
95,984✔
2011
    } else {
2012
      dst.columnData = pInfo;
48,182✔
2013
      dst.numOfRows = rowIdx;
48,182✔
2014
      dst.colAlloced = false;
48,182✔
2015
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
48,182✔
2016
    }
2017
    ++idx;
144,166✔
2018
    // TODO sclFreeParam(&dst);
2019
    nodesDestroyNode(pNode);
144,166✔
2020
    if (code != 0) break;
144,166✔
2021
  }
2022
  if (code == TSDB_CODE_SUCCESS) {
48,182✔
2023
    (*pRes)->info.rows++;
48,182✔
2024
  }
2025
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
48,182✔
2026
  return code;
48,182✔
2027
}
2028

2029
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
333,635✔
2030
  int32_t      code = 0;
333,635✔
2031
  const char*  pVal = NULL;
333,635✔
2032
  SScalarParam dst = {0};
333,635✔
2033
  int32_t      len = 0;
333,635✔
2034
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
333,635✔
2035
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
333,635✔
2036
  // execute the expr
2037
  switch (pExpr->type) {
333,635✔
UNCOV
2038
    case QUERY_NODE_VALUE: {
×
2039
      SValueNode* pValue = (SValueNode*)pExpr;
×
2040
      int32_t     type = pValue->node.resType.type;
×
2041
      if (!IS_STR_DATA_TYPE(type)) {
×
UNCOV
2042
        qError("invalid sub tb expr with non-str type");
×
UNCOV
2043
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2044
        break;
×
2045
      }
2046
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2047
      if (pTmp == NULL) {
×
UNCOV
2048
        qError("invalid sub tb expr with null value");
×
UNCOV
2049
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2050
        break;
×
2051
      }
2052
      pVal = varDataVal(pTmp);
×
2053
      len = varDataLen(pTmp);
×
UNCOV
2054
    } break;
×
2055
    case QUERY_NODE_FUNCTION: {
333,635✔
2056
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
333,635✔
2057
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
333,635✔
UNCOV
2058
        qError("invalid sub tb expr with non-str type func");
×
UNCOV
2059
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2060
        break;
×
2061
      }
2062
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
333,635✔
2063
      if (!pCol) {
333,635✔
2064
        code = terrno;
×
2065
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2066
        break;
×
2067
      }
2068

2069
      pCol->hasNull = true;
333,635✔
2070
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
333,635✔
2071
      pCol->info.colId = 0;
333,635✔
2072
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
333,635✔
2073
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
333,635✔
2074
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
333,635✔
2075
      code = colInfoDataEnsureCapacity(pCol, 1, true);
333,635✔
2076
      if (code != 0) {
333,635✔
UNCOV
2077
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2078
        taosMemoryFree(pCol);
×
UNCOV
2079
        break;
×
2080
      }
2081
      dst.columnData = pCol;
333,635✔
2082
      dst.numOfRows = 1;
333,635✔
2083
      dst.colAlloced = true;
333,635✔
2084
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
333,635✔
2085
      if (colDataIsNull_var(dst.columnData, 0)) {
333,279✔
2086
        qInfo("invalid sub tb expr with null value");
4,628✔
2087
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
5,340✔
2088
      }
2089
      if (code == 0) {
333,635✔
2090
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
328,295✔
2091
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
328,295✔
2092
      }
2093
    } break;
333,205✔
UNCOV
2094
    default:
×
UNCOV
2095
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2096
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2097
      break;
×
2098
  }
2099
  if (code == 0) {
333,205✔
2100
    if (!pVal || len == 0) {
327,865✔
UNCOV
2101
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2102
      code = TSDB_CODE_INVALID_PARA;
×
2103
    }
2104
    if(len > TSDB_TABLE_NAME_LEN - 1) {
327,865✔
2105
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
800✔
2106
      len = TSDB_TABLE_NAME_LEN - 1;
800✔
2107
    }
2108

2109
    memcpy(tbname, pVal, len);
327,865✔
2110
    tbname[len] = '\0';  // ensure null terminated
327,865✔
2111
    if (NULL != strchr(tbname, '.')) {
328,295✔
UNCOV
2112
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
UNCOV
2113
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2114
    }
2115
  }
2116
  // TODO free dst
2117
  sclFreeParam(&dst);
333,635✔
2118
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
333,635✔
2119
  return code;
333,635✔
2120
}
2121

2122
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
319,344✔
2123
  if (pParam) {
319,344✔
2124
    if (pParam->tbname) {
319,344✔
2125
      taosMemFree(pParam->tbname);
319,344✔
2126
      pParam->tbname = NULL;
319,344✔
2127
    }
2128
    if (pParam->stbname) {
319,344✔
2129
      taosMemFree(pParam->stbname);
319,344✔
2130
      pParam->stbname = NULL;
319,344✔
2131
    }
2132
    if (pParam->dbFName) {
319,344✔
2133
      taosMemFree(pParam->dbFName);
319,344✔
2134
      pParam->dbFName = NULL;
319,344✔
2135
    }
2136
    if (pParam->pFields) {
319,344✔
2137
      taosArrayDestroy(pParam->pFields);
319,344✔
2138
      pParam->pFields = NULL;
319,344✔
2139
    }
2140
    if (pParam->pTagFields) {
319,344✔
2141
      taosArrayDestroy(pParam->pTagFields);
206,373✔
2142
      pParam->pTagFields = NULL;
206,373✔
2143
    }
2144
    taosMemFree(pParam);
319,344✔
2145
  }
2146
}
319,344✔
2147

2148
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
319,344✔
2149
  int32_t code = 0, lino = 0;
319,344✔
2150
  if (ppDst == NULL || pSrc == NULL) {
319,344✔
UNCOV
2151
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2152
  }
2153
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
319,344✔
2154
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
319,344✔
2155

2156
  (*ppDst)->suid = pSrc->suid;
319,344✔
2157
  (*ppDst)->sver = pSrc->sver;
319,344✔
2158
  (*ppDst)->tbType = pSrc->tbType;
319,344✔
2159
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
319,344✔
2160
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
319,344✔
2161

2162
  if (pSrc->stbname) {
319,344✔
2163
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
319,344✔
2164
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
319,344✔
2165
  }
2166

2167
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
318,947✔
2168
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
318,947✔
2169

2170
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
318,947✔
2171

2172
  if (pSrc->pFields && pSrc->pFields->size > 0) {
319,344✔
2173
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
318,947✔
2174
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
319,344✔
2175
  } else {
2176
    (*ppDst)->pFields = NULL;
×
2177
  }
2178
  
2179
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
318,947✔
2180
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
206,373✔
2181
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
206,373✔
2182
  } else {
2183
    (*ppDst)->pTagFields = NULL;
112,574✔
2184
  }
2185

2186
_exit:
319,344✔
2187

2188
  if (code != 0) {
319,344✔
UNCOV
2189
    if (*ppDst) {
×
2190
      destroyStreamInserterParam(*ppDst);
×
2191
      *ppDst = NULL;
×
2192
    }
2193
    
UNCOV
2194
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2195
  }
2196
  return code;
319,344✔
2197
}
2198

2199
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
431✔
2200
  return doDropStreamTable(pMsgCb, pOutput, pReq);
431✔
2201
}
2202

UNCOV
2203
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
UNCOV
2204
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2205
}
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