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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 hits per line

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

68.61
/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) {
687,265✔
44
  gExecInfo.dnode = pDnode;
687,265✔
45
  gExecInfo.getMnode = getMnode;
687,265✔
46
  gExecInfo.getDnodeId = getDnodeId;
687,265✔
47
  return;
687,265✔
48
}
49

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

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

64
static void initRefPool() {
640,946✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
640,946✔
66
  (void)atexit(cleanupRefPool);
640,946✔
67
}
640,946✔
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,894,126✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
15,894,126✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
15,893,828✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,365,866✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,365,866✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,365,788✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,365,607✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
55,409✔
158
        if (code) {
55,409✔
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,528,224✔
166
  }
167

168
  return 0;
10,782,837✔
169
}
170

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

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,783,115✔
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,763✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
141,763✔
209
    SExecTaskInfo* pTaskInfo = NULL;
22,763✔
210

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,395,268✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,276,268✔
249
    if (pSlotDesc->output) {
1,276,268✔
250
      ++(*numOfCols);
1,276,182✔
251
    }
252
  }
253

254
  return pTaskInfo;
119,000✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
749,494✔
258
  if (streamInserterParam == NULL) {
749,494✔
259
    return TSDB_CODE_SUCCESS;
427,881✔
260
  }
261

262
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
321,613✔
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) {
321,613✔
268
    stError("insertParam: invalid db/table name");
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

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

278
  return TSDB_CODE_SUCCESS;
321,613✔
279
}
280

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

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

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

316
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
321,613✔
317
    if (NULL == pInserterParam) {
321,613✔
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);
321,613✔
323
    TSDB_CHECK_CODE(code, lino, _error);
321,613✔
324
    
325
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
321,613✔
326
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
321,613✔
327

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
321,613✔
329
    if (code) {
321,613✔
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,
749,494✔
334
         tstrerror(code));
335

336
_error:
146,289✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
5,672,425✔
348
  if (pInfo == NULL) {
5,672,425✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,672,425✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,672,425✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,672,823✔
354
    return false;
4,836✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
5,667,987✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
5,406,990✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
11,074,977✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

382
  void*           info = pOperator->info;
5,667,987✔
383
  STableScanBase* pScanBaseInfo = NULL;
5,667,589✔
384

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,667,589✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
260,997✔
387
    setReadHandle(handle, pScanBaseInfo);
260,997✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
5,406,592✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,862,306✔
390
    setReadHandle(handle, pScanBaseInfo);
4,862,306✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
749,494✔
405

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

421
  return code;
749,494✔
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,
68,454✔
431
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
432
  int32_t code = TSDB_CODE_SUCCESS;
68,454✔
433
  int32_t lino = 0;
68,454✔
434
  int8_t  locked = 0;
68,454✔
435
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
68,454✔
436

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
68,454✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
68,454✔
449

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

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

459
  locked = 1;
68,454✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
137,934✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
69,480✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
69,480✔
463

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

470
    tDecoderClear(&mr.coder);
69,480✔
471

472
    if (mr.me.type == TSDB_SUPER_TABLE) {
69,480✔
473
      continue;
×
474
    } else {
475
      if (type == TSDB_SUPER_TABLE) {
69,480✔
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) {
69,480✔
478
          continue;
980✔
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};
68,500✔
490
    if (pScanInfo->pTagCond != NULL) {
68,500✔
491
      // tb_uid_t id = mr.me.uid;
492
      item.check = 1;
59,244✔
493
    }
494
    if (taosArrayPush(tUid, &item) == NULL) {
68,500✔
495
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
496
    }
497
  }
498

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

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

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

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

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

526
  // handle multiple partition
527

528
_end:
68,454✔
529

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

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

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

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

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

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

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

579
    bool   assignUid = false;
68,454✔
580
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
68,454✔
581
    char*  keyBuf = NULL;
68,454✔
582
    if (bufLen > 0) {
68,454✔
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;
68,454✔
592
    taosWLockLatch(&pTaskInfo->lock);
68,454✔
593

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

604
      if (bufLen > 0) {
38,879✔
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,879✔
620
      if (code != TSDB_CODE_SUCCESS) {
38,879✔
621
        taosMemoryFree(keyBuf);
×
622
        taosArrayDestroy(qa);
×
623
        taosWUnLockLatch(&pTaskInfo->lock);
×
624
        return code;
×
625
      }
626
    }
627

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

633
    taosArrayDestroy(qa);
68,454✔
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);
207✔
636
    taosWLockLatch(&pTaskInfo->lock);
207✔
637
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
207✔
638
    taosWUnLockLatch(&pTaskInfo->lock);
207✔
639
  }
640

641
  return code;
68,661✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
270,228,562✔
650
    return TSDB_CODE_INVALID_PARA;
4,916✔
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
270,223,646✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
270,223,646✔
655
    return TSDB_CODE_SUCCESS;
155,507,169✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
114,704,871✔
665
  *tversion = pSchemaInfo->tversion;
114,724,303✔
666
  *rversion = pSchemaInfo->rversion;
114,711,863✔
667
  if (pSchemaInfo->dbname) {
114,727,946✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
114,722,106✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
114,737,613✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
114,727,662✔
674
  } else {
675
    tableName[0] = 0;
365✔
676
  }
677

678
  *tbGet = true;
114,737,480✔
679

680
  return TSDB_CODE_SUCCESS;
114,730,005✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
155,501,278✔
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) {
9,506,591✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
9,506,591✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
9,506,591✔
695
}
9,506,591✔
696

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

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

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

710
  readHandle->uid = 0;
157,409,191✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
157,418,530✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
157,373,845✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
76,960✔
714
    goto _error;
27,485✔
715
  }
716

717
  if (handle) {
157,301,997✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
157,215,599✔
719
    void*           pSinkManager = NULL;
157,232,627✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
157,123,538✔
721
    if (code != TSDB_CODE_SUCCESS) {
157,226,408✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
157,226,408✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
157,227,250✔
728
    if (code != TSDB_CODE_SUCCESS) {
157,136,958✔
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;
157,136,958✔
735
    if (readHandle->localExec) {
157,211,885✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
67,971✔
737
      if (code != TSDB_CODE_SUCCESS) {
67,971✔
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,
157,181,240✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
157,241,029✔
748
    if (code) {
157,159,267✔
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,
157,331,228✔
754
         tstrerror(code));
755

756
_error:
12,144,380✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
157,413,193✔
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,
182,485,126✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
182,485,126✔
769
  int32_t        lino = 0;
182,485,126✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
182,485,126✔
771
  int64_t        threadId = taosGetSelfPthreadId();
182,485,126✔
772

773
  if (pLocal) {
182,478,485✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
176,355,786✔
775
  }
776

777
  taosArrayClear(pResList);
182,466,329✔
778

779
  int64_t curOwner = 0;
182,473,770✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
182,473,770✔
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) {
182,465,661✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
155,526,064✔
788
  }
789

790
  if (isTaskKilled(pTaskInfo)) {
182,478,807✔
791
    atomic_store_64(&pTaskInfo->owner, 0);
120✔
792
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
120✔
793
    return pTaskInfo->code;
120✔
794
  }
795

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
182,478,624✔
798
  if (ret != TSDB_CODE_SUCCESS) {
182,700,217✔
799
    pTaskInfo->code = ret;
246,703✔
800
    (void)cleanUpUdfs();
246,703✔
801

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

805
    return pTaskInfo->code;
246,703✔
806
  }
807

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
182,453,514✔
809

810
  int32_t      current = 0;
182,455,250✔
811
  SSDataBlock* pRes = NULL;
182,455,250✔
812
  int64_t      st = taosGetTimestampUs();
182,487,617✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
182,487,617✔
815
    pTaskInfo->paramSet = true;
9,506,591✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
9,506,591✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
172,976,462✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
182,242,074✔
822
  code = blockDataCheck(pRes);
182,242,074✔
823
  QUERY_CHECK_CODE(code, lino, _end);
182,239,430✔
824

825
  if (pRes == NULL) {
182,239,430✔
826
    st = taosGetTimestampUs();
32,045,624✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
182,239,691✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
182,239,480✔
831
    rowsThreshold = 4096;
181,495,754✔
832
  }
833

834
  int32_t blockIndex = 0;
182,240,545✔
835
  while (pRes != NULL) {
554,083,452✔
836
    SSDataBlock* p = NULL;
392,278,965✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
392,268,883✔
838
      SSDataBlock* p1 = NULL;
285,119,013✔
839
      code = createOneDataBlock(pRes, true, &p1);
285,119,093✔
840
      QUERY_CHECK_CODE(code, lino, _end);
285,115,632✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
285,115,632✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
285,120,639✔
844
      p = p1;
285,120,639✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
107,164,700✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
107,177,727✔
848

849
      p = *(SSDataBlock**)tmp;
107,177,727✔
850
      code = copyDataBlock(p, pRes);
107,180,170✔
851
      QUERY_CHECK_CODE(code, lino, _end);
107,176,735✔
852
    }
853

854
    blockIndex += 1;
392,296,872✔
855

856
    current += p->info.rows;
392,296,872✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
392,296,327✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
392,271,278✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
392,271,278✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
392,271,278✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
371,832,551✔
867
    QUERY_CHECK_CODE(code, lino, _end);
371,813,209✔
868
    code = blockDataCheck(pRes);
371,813,209✔
869
    QUERY_CHECK_CODE(code, lino, _end);
371,863,955✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
182,243,214✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
741,761✔
874
  }
875

876
  *hasMore = (pRes != NULL);
182,246,006✔
877
  uint64_t el = (taosGetTimestampUs() - st);
182,239,675✔
878

879
  pTaskInfo->cost.elapsedTime += el;
182,239,675✔
880
  if (NULL == pRes) {
182,242,519✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
161,803,792✔
882
  }
883

884
_end:
182,129,399✔
885
  (void)cleanUpUdfs();
182,239,693✔
886

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

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

897
  return pTaskInfo->code;
182,249,437✔
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,143,920✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,143,920✔
916
  int64_t        threadId = taosGetSelfPthreadId();
39,143,920✔
917
  int64_t        curOwner = 0;
39,145,525✔
918

919
  *pRes = NULL;
39,145,525✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
39,145,525✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
39,145,525✔
924
  if (isKilled) {
39,145,418✔
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,145,418✔
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,145,204✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
39,145,311✔
941

942
  if (pTaskInfo->cost.start == 0) {
39,145,632✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
94,511✔
944
  }
945

946
  // error occurs, record the error code and return to client
947
  int32_t ret = setjmp(pTaskInfo->env);
39,145,739✔
948
  if (ret != TSDB_CODE_SUCCESS) {
39,144,752✔
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,144,752✔
957

958
  int64_t st = taosGetTimestampUs();
39,144,883✔
959
  int32_t code = TSDB_CODE_SUCCESS;
39,144,883✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
39,144,883✔
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,144,348✔
965
  }
966
  if (code) {
39,134,118✔
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,134,118✔
972
  if (code) {
39,145,463✔
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,135,509✔
978

979
  pTaskInfo->cost.elapsedTime += el;
39,135,509✔
980
  if (NULL == *pRes) {
39,143,382✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
4,023,803✔
982
  }
983

984
  (void)cleanUpUdfs();
39,144,069✔
985

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

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
39,144,569✔
993
  return pTaskInfo->code;
39,145,632✔
994
}
995

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

1001
  if (!tmp) {
51,576,776✔
1002
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1003
    return terrno;
×
1004
  }
1005
  return TSDB_CODE_SUCCESS;
51,576,776✔
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) {
18,130✔
1031
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
18,130✔
1032

1033
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
18,130✔
1034
  for (int32_t i = 0; i < num; ++i) {
22,354✔
1035
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
4,224✔
1036
    if (!pStop) {
4,224✔
1037
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1038
      continue;
×
1039
    }
1040
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
4,224✔
1041
    if (pExchangeInfo) {
4,224✔
1042
      int32_t code = tsem_post(&pExchangeInfo->ready);
4,224✔
1043
      if (code != TSDB_CODE_SUCCESS) {
4,224✔
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,224✔
1047
      }
1048
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
4,224✔
1049
      if (code != TSDB_CODE_SUCCESS) {
4,224✔
1050
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1051
      }
1052
    }
1053
  }
1054

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
18,130✔
1067
  qStopTaskOperators(pTaskInfo);
18,130✔
1068

1069
  return TSDB_CODE_SUCCESS;
18,130✔
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) {
158,164,645✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
158,164,645✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
158,165,588✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
158,163,807✔
1130
  if (pSummary->pRecoder != NULL) {
158,158,557✔
1131
    qDebug(
113,926,841✔
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,
44,236,509✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
158,163,379✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
165,421,881✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
165,421,881✔
1146
  if (pTaskInfo == NULL) {
165,421,881✔
1147
    return;
7,259,699✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
158,162,182✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
158,165,103✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
158,165,550✔
1157
  doDestroyTask(pTaskInfo);
158,163,909✔
1158
}
1159

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

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

1169
  while (1) {
116,274✔
1170
    uint16_t type = pOperator->operatorType;
235,274✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
235,274✔
1172
      *scanner = pOperator->info;
119,000✔
1173
      break;
119,000✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
116,274✔
1176
    }
1177
  }
1178
}
119,000✔
1179

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

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

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

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

1200
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
4,464,212✔
1201
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,464,212✔
1202
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
4,464,212✔
1203
  return 0;
4,464,212✔
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) {
143,993✔
1212
  memset(pCond, 0, sizeof(SQueryTableDataCond));
143,993✔
1213
  pCond->order = TSDB_ORDER_ASC;
143,993✔
1214
  pCond->numOfCols = pMtInfo->schema->nCols;
144,047✔
1215
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
144,047✔
1216
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
144,155✔
1217
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
144,155✔
1218
    taosMemoryFreeClear(pCond->colList);
137✔
1219
    taosMemoryFreeClear(pCond->pSlotList);
×
1220
    return terrno;
×
1221
  }
1222

1223
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
144,018✔
1224
  pCond->suid = pMtInfo->suid;
144,234✔
1225
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
144,155✔
1226
  pCond->startVersion = -1;
144,317✔
1227
  pCond->endVersion = sContext->snapVersion;
144,263✔
1228

1229
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
923,282✔
1230
    SColumnInfo* pColInfo = &pCond->colList[i];
779,044✔
1231
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
779,019✔
1232
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
779,264✔
1233
    if (pMtInfo->pExtSchemas != NULL) {
779,318✔
1234
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,484✔
1235
    }
1236
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
779,372✔
1237
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
779,102✔
1238

1239
    pCond->pSlotList[i] = i;
779,210✔
1240
  }
1241

1242
  return TSDB_CODE_SUCCESS;
144,209✔
1243
}
1244

1245
void qStreamSetOpen(qTaskInfo_t tinfo) {
38,857,962✔
1246
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
38,857,962✔
1247
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
38,857,962✔
1248
  pOperator->status = OP_NOT_OPENED;
38,861,034✔
1249
}
38,860,689✔
1250

1251
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,308,622✔
1252
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,308,622✔
1253
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,308,622✔
1254
}
4,308,699✔
1255

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

1261
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,521,196✔
1262
  const char*    id = GET_TASKID(pTaskInfo);
4,520,851✔
1263

1264
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,520,656✔
1265
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,247,626✔
1266
    if (pOperator == NULL || code != 0) {
4,247,334✔
1267
      return code;
×
1268
    }
1269

1270
    SStreamScanInfo* pInfo = pOperator->info;
4,247,459✔
1271
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,246,573✔
1272
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,247,609✔
1273
    walReaderVerifyOffset(pWalReader, pOffset);
4,247,758✔
1274
  }
1275
  // if pOffset equal to current offset, means continue consume
1276
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,520,236✔
1277
    return 0;
4,079,971✔
1278
  }
1279

1280
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
440,069✔
1281
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
292,273✔
1282
    if (pOperator == NULL || code != 0) {
291,990✔
1283
      return code;
78✔
1284
    }
1285

1286
    SStreamScanInfo* pInfo = pOperator->info;
291,912✔
1287
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
292,005✔
1288
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
291,865✔
1289
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
291,950✔
1290

1291
    if (pOffset->type == TMQ_OFFSET__LOG) {
292,004✔
1292
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
231,959✔
1293
      pScanBaseInfo->dataReader = NULL;
231,825✔
1294

1295
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
231,854✔
1296
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
231,930✔
1297
      walReaderVerifyOffset(pWalReader, pOffset);
231,905✔
1298
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
231,959✔
1299
      if (code < 0) {
231,959✔
1300
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,734✔
1301
        return code;
7,734✔
1302
      }
1303
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
60,102✔
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;
60,314✔
1307
      int64_t ts = pOffset->ts;
60,314✔
1308
      int32_t index = 0;
60,165✔
1309

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

1320
      if (uid == 0) {
59,969✔
1321
        if (numOfTables != 0) {
59,264✔
1322
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
10,180✔
1323
          if (!tmp) {
10,180✔
1324
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1325
            taosRUnLockLatch(&pTaskInfo->lock);
×
1326
            return terrno;
×
1327
          }
1328
          if (tmp) uid = tmp->uid;
10,180✔
1329
          ts = INT64_MIN;
10,180✔
1330
          pScanInfo->currentTable = 0;
10,180✔
1331
        } else {
1332
          taosRUnLockLatch(&pTaskInfo->lock);
49,084✔
1333
          qError("no table in table list, %s", id);
49,148✔
1334
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
49,148✔
1335
        }
1336
      }
1337
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
10,885✔
1338

1339
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
11,166✔
1340
             pInfo->pTableScanOp->resultInfo.totalRows);
1341
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
11,166✔
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,166✔
1347
      taosRUnLockLatch(&pTaskInfo->lock);
11,166✔
1348

1349
      if (index >= 0) {
11,166✔
1350
        pScanInfo->currentTable = index;
11,166✔
1351
      } else {
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);
1354
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1355
      }
1356

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

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

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

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

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

1395
      // restore the key value
1396
      pScanBaseInfo->cond.twindows.skey = oldSkey;
11,235✔
1397
    } else {
1398
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
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) {
147,796✔
1404
      SStreamRawScanInfo* pInfo = pOperator->info;
144,500✔
1405
      SSnapContext*       sContext = pInfo->sContext;
144,500✔
1406
      SOperatorInfo*      p = NULL;
144,500✔
1407

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

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

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

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

1429
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
144,284✔
1430
      tableListClear(pTableListInfo);
144,436✔
1431

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

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

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

1457
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
144,317✔
1458
      if (!pList) {
144,263✔
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;
144,263✔
1464
      code = tableListGetSize(pTableListInfo, &size);
144,263✔
1465
      if (code != TSDB_CODE_SUCCESS) {
144,317✔
1466
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1467
        destroyMetaTableInfo(&mtInfo);
1468
        return code;
×
1469
      }
1470

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

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

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

1505
end:
382,805✔
1506
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
383,187✔
1507
  return 0;
383,187✔
1508
}
1509

1510
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
113,008,501✔
1511
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
113,008,501✔
1512
  if (pMsg->info.ahandle == NULL) {
113,015,965✔
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, 
113,003,448✔
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};
113,009,245✔
1521

1522
  if (pMsg->contLen > 0) {
113,013,293✔
1523
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
113,001,084✔
1524
    if (buf.pData == NULL) {
112,996,486✔
1525
      pMsg->code = terrno;
×
1526
    } else {
1527
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
112,996,486✔
1528
    }
1529
  }
1530

1531
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
113,015,068✔
1532
  rpcFreeCont(pMsg->pCont);
113,023,282✔
1533
  destroySendMsgInfo(pSendInfo);
113,009,697✔
1534
}
1535

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

1542
  code = getTableListInfo(pTaskInfo, &plist);
×
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

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

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

1564
  taosArrayDestroy(plist);
×
1565

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

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

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

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

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

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

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

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

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

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

1631
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1632
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
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;
×
1643
  pTaskInfo->code = 0;
×
1644
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1645
}
×
1646

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

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

1658
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
36,385,474✔
1659
  int32_t code = 0;
36,385,474✔
1660

1661
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
36,385,474✔
1662
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
36,382,812✔
1663

1664
  if (pOper->fpSet.resetStateFn) {
36,382,005✔
1665
    code = pOper->fpSet.resetStateFn(pOper);
36,382,386✔
1666
  }
1667
  pOper->status = OP_NOT_OPENED;
36,383,361✔
1668
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
61,201,939✔
1669
    code = clearStatesForOperator(pOper->pDownstream[i]);
24,814,332✔
1670
  }
1671
  return code;
36,388,412✔
1672
}
1673

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

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

1688
  *ppRes = NULL;
15,641,597✔
1689

1690
  // todo extract method
1691
  taosRLockLatch(&pTaskInfo->lock);
15,641,597✔
1692
  bool isKilled = isTaskKilled(pTaskInfo);
15,642,006✔
1693
  if (isKilled) {
15,641,234✔
1694
    // clearStreamBlock(pTaskInfo->pRoot);
1695
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1696

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

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

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

1709
  pTaskInfo->owner = threadId;
15,641,234✔
1710
  taosRUnLockLatch(&pTaskInfo->lock);
15,642,006✔
1711

1712
  if (pTaskInfo->cost.start == 0) {
15,641,234✔
1713
    pTaskInfo->cost.start = taosGetTimestampUs();
329,585✔
1714
  }
1715

1716
  // error occurs, record the error code and return to client
1717
  int32_t ret = setjmp(pTaskInfo->env);
15,641,234✔
1718
  if (ret != TSDB_CODE_SUCCESS) {
17,570,877✔
1719
    pTaskInfo->code = ret;
1,928,871✔
1720
    (void)cleanUpUdfs();
1,928,871✔
1721
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,928,871✔
1722
    atomic_store_64(&pTaskInfo->owner, 0);
1,928,871✔
1723
    return pTaskInfo->code;
1,928,871✔
1724
  }
1725

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

1728
  int64_t st = taosGetTimestampUs();
15,641,603✔
1729

1730
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
15,641,603✔
1731
  if (code) {
13,713,135✔
1732
    pTaskInfo->code = code;
×
1733
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1734
  } else {
1735
    *finished = *ppRes == NULL;
13,713,135✔
1736
    code = blockDataCheck(*ppRes);
13,713,135✔
1737
  }
1738
  if (code) {
13,713,135✔
1739
    pTaskInfo->code = code;
×
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);
13,713,135✔
1744

1745
  pTaskInfo->cost.elapsedTime += el;
13,713,135✔
1746
  if (NULL == *ppRes) {
13,713,135✔
1747
    *useconds = pTaskInfo->cost.elapsedTime;
9,049,608✔
1748
  }
1749

1750
  (void)cleanUpUdfs();
13,713,135✔
1751

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

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

1758
  atomic_store_64(&pTaskInfo->owner, 0);
13,713,135✔
1759
  return pTaskInfo->code;
13,713,135✔
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,
322,235✔
1768
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1769
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1770
  int32_t code = 0;                                        
322,235✔
1771
  if (*pTableListInfo != NULL) {
322,235✔
1772
    qDebug("table list already exists, no need to create again");
×
1773
    goto end;
×
1774
  }
1775
  STableListInfo* pList = tableListCreate();
322,235✔
1776
  if (pList == NULL) {
322,235✔
1777
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1778
    code = terrno;
×
1779
    goto end;
×
1780
  }
1781

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

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

1794
end:
322,235✔
1795
  return 0;
322,235✔
1796
}
1797

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

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

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

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

1849
  taosArrayClear(uidList);
165,170✔
1850
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
330,340✔
1851
    void* tmp = taosArrayGet(uidListCopy, i);
165,170✔
1852
    if (tmp == NULL) {
165,170✔
1853
      continue;
×
1854
    }
1855
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
165,170✔
1856
    if (slot == NULL) {
165,170✔
1857
      if (taosArrayPush(uidList, tmp) == NULL) {
6,179✔
1858
        code = terrno;
×
1859
        goto end;
×
1860
      }
1861
    }
1862
  }
1863
end:
165,170✔
1864
  taosArrayDestroy(uidListCopy);
165,170✔
1865
  tableListDestroy(pList);
165,170✔
1866
  return code;
165,170✔
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); }
322,235✔
1891
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
321,850✔
1892
  STableListInfo* pList = pTableListInfo;
321,850✔
1893
  return pList->pTableList;
321,850✔
1894
}
1895

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2172
  if (pSrc->pFields && pSrc->pFields->size > 0) {
321,613✔
2173
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
321,613✔
2174
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
321,613✔
2175
  } else {
UNCOV
2176
    (*ppDst)->pFields = NULL;
×
2177
  }
2178
  
2179
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
321,613✔
2180
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
209,251✔
2181
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
209,650✔
2182
  } else {
2183
    (*ppDst)->pTagFields = NULL;
112,362✔
2184
  }
2185

2186
_exit:
321,613✔
2187

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

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

2203
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
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