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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

108937395.15 hits per line

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

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

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

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

64
static void initRefPool() {
644,559✔
65
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
644,559✔
66
  (void)atexit(cleanupRefPool);
644,559✔
67
}
644,559✔
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) {
16,367,403✔
151
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
16,367,403✔
152
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
16,368,266✔
153
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,313,707✔
154
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,313,707✔
155
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,313,155✔
156
      if (pScanInfo->base.dataReader != NULL) {
4,313,068✔
157
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
59,475✔
158
        if (code) {
59,404✔
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);
12,053,994✔
166
  }
167

168
  return 0;
11,382,360✔
169
}
170

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

176
  // set the idstr for tsdbReader
177
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
11,382,337✔
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,
144,731✔
207
                                     uint64_t id) {
208
  if (msg == NULL) {  // create raw scan
144,731✔
209
    SExecTaskInfo* pTaskInfo = NULL;
24,208✔
210

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

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

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

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

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

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

246
  SNode* pNode;
247
  FOREACH(pNode, pDescNode->pSlots) {
1,409,841✔
248
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
1,288,712✔
249
    if (pSlotDesc->output) {
1,288,712✔
250
      ++(*numOfCols);
1,288,610✔
251
    }
252
  }
253

254
  return pTaskInfo;
121,129✔
255
}
256

257
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
659,294✔
258
  if (streamInserterParam == NULL) {
659,294✔
259
    return TSDB_CODE_SUCCESS;
358,094✔
260
  }
261

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

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

278
  return TSDB_CODE_SUCCESS;
301,200✔
279
}
280

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

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

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

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

328
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
301,200✔
329
    if (code) {
301,200✔
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,
659,294✔
334
         tstrerror(code));
335

336
_error:
148,815✔
337

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

347
bool qNeedReset(qTaskInfo_t pInfo) {
6,414,307✔
348
  if (pInfo == NULL) {
6,414,307✔
349
    return false;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
6,414,307✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
6,414,307✔
353
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
6,414,731✔
354
    return false;
4,788✔
355
  }
356
  int32_t node = nodeType(pOperator->pPhyNode);
6,409,943✔
357
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
6,135,716✔
358
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
12,545,659✔
359
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
360
}
361

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

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

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

382
  void*           info = pOperator->info;
6,409,943✔
383
  STableScanBase* pScanBaseInfo = NULL;
6,409,943✔
384

385
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
6,409,943✔
386
    pScanBaseInfo = &((STableScanInfo*)info)->base;
274,227✔
387
    setReadHandle(handle, pScanBaseInfo);
274,227✔
388
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
6,135,716✔
389
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
5,471,820✔
390
    setReadHandle(handle, pScanBaseInfo);
5,471,820✔
391
  }
392

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

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

404
  *pTaskInfo = NULL;
659,294✔
405

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

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

437
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
72,325✔
438

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

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

448
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
72,325✔
449

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

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

459
  locked = 1;
72,325✔
460
  for (int32_t i = 0; i < numOfUids; ++i) {
144,650✔
461
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
72,325✔
462
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
72,325✔
463

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

470
    tDecoderClear(&mr.coder);
72,325✔
471

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

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

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

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

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

517
      if (!qualified) {
64,790✔
518
        continue;
32,395✔
519
      }
520
    }
521

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

526
  // handle multiple partition
527

528
_end:
72,325✔
529

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

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

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

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

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

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

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

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

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

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

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

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

641
  return code;
72,983✔
642
}
643

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

649
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
263,361,624✔
650
    return TSDB_CODE_INVALID_PARA;
8,684✔
651
  }
652
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
263,353,225✔
653

654
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
263,353,225✔
655
    return TSDB_CODE_SUCCESS;
151,206,259✔
656
  }
657

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

664
  *sversion = pSchemaInfo->sw->version;
112,137,251✔
665
  *tversion = pSchemaInfo->tversion;
112,162,615✔
666
  *rversion = pSchemaInfo->rversion;
112,151,046✔
667
  if (pSchemaInfo->dbname) {
112,166,334✔
668
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
112,162,909✔
669
  } else {
670
    dbName[0] = 0;
×
671
  }
672
  if (pSchemaInfo->tablename) {
112,175,942✔
673
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
112,167,408✔
674
  } else {
675
    tableName[0] = 0;
51✔
676
  }
677

678
  *tbGet = true;
112,176,132✔
679

680
  return TSDB_CODE_SUCCESS;
112,169,973✔
681
}
682

683
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
151,199,881✔
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) {
3,913,266✔
693
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
3,913,266✔
694
  ((SExecTaskInfo*)tinfo)->paramSet = false;
3,913,266✔
695
}
3,913,266✔
696

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

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

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

710
  readHandle->uid = 0;
153,098,942✔
711
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
153,123,244✔
712
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
153,001,789✔
713
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
101,118✔
714
    goto _error;
27,469✔
715
  }
716

717
  if (handle) {
152,927,031✔
718
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
152,842,940✔
719
    void*           pSinkManager = NULL;
152,874,340✔
720
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
152,685,750✔
721
    if (code != TSDB_CODE_SUCCESS) {
152,854,317✔
722
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
723
      goto _error;
×
724
    }
725

726
    void* pSinkParam = NULL;
152,854,317✔
727
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
152,857,847✔
728
    if (code != TSDB_CODE_SUCCESS) {
152,675,269✔
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;
152,675,269✔
735
    if (readHandle->localExec) {
152,825,085✔
736
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
83,815✔
737
      if (code != TSDB_CODE_SUCCESS) {
83,815✔
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,
152,777,196✔
747
                              (*pTask)->id.str, pSubplan->processOneBlock);
152,898,143✔
748
    if (code) {
152,726,565✔
749
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
635✔
750
    }
751
  }
752

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

756
_error:
9,685,584✔
757
  // if failed to add ref for all tables in this query, abort current query
758
  return code;
153,114,701✔
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,
174,729,415✔
767
                     bool processOneBlock) {
768
  int32_t        code = TSDB_CODE_SUCCESS;
174,729,415✔
769
  int32_t        lino = 0;
174,729,415✔
770
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
174,729,415✔
771
  int64_t        threadId = taosGetSelfPthreadId();
174,729,415✔
772

773
  if (pLocal) {
174,721,953✔
774
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
167,921,692✔
775
  }
776

777
  taosArrayClear(pResList);
174,704,071✔
778

779
  int64_t curOwner = 0;
174,710,742✔
780
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
174,710,742✔
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) {
174,718,569✔
787
    pTaskInfo->cost.start = taosGetTimestampUs();
151,522,494✔
788
  }
789

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

796
  // error occurs, record the error code and return to client
797
  int32_t ret = setjmp(pTaskInfo->env);
174,717,474✔
798
  if (ret != TSDB_CODE_SUCCESS) {
174,938,411✔
799
    pTaskInfo->code = ret;
248,805✔
800
    (void)cleanUpUdfs();
248,805✔
801

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

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

808
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
174,689,606✔
809

810
  int32_t      current = 0;
174,691,438✔
811
  SSDataBlock* pRes = NULL;
174,691,438✔
812
  int64_t      st = taosGetTimestampUs();
174,730,971✔
813

814
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
174,730,971✔
815
    pTaskInfo->paramSet = true;
3,913,266✔
816
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
3,913,266✔
817
  } else {
818
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
170,801,725✔
819
  }
820

821
  QUERY_CHECK_CODE(code, lino, _end);
174,477,753✔
822
  code = blockDataCheck(pRes);
174,477,753✔
823
  QUERY_CHECK_CODE(code, lino, _end);
174,481,015✔
824

825
  if (pRes == NULL) {
174,481,015✔
826
    st = taosGetTimestampUs();
29,979,598✔
827
  }
828

829
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
174,481,175✔
830
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
174,479,756✔
831
    rowsThreshold = 4096;
173,681,634✔
832
  }
833

834
  int32_t blockIndex = 0;
174,480,575✔
835
  while (pRes != NULL) {
553,700,525✔
836
    SSDataBlock* p = NULL;
395,720,318✔
837
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
395,704,415✔
838
      SSDataBlock* p1 = NULL;
289,794,316✔
839
      code = createOneDataBlock(pRes, true, &p1);
289,795,583✔
840
      QUERY_CHECK_CODE(code, lino, _end);
289,783,718✔
841

842
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
289,783,718✔
843
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
289,792,160✔
844
      p = p1;
289,792,160✔
845
    } else {
846
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
105,940,206✔
847
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
105,940,271✔
848

849
      p = *(SSDataBlock**)tmp;
105,940,271✔
850
      code = copyDataBlock(p, pRes);
105,940,476✔
851
      QUERY_CHECK_CODE(code, lino, _end);
105,939,335✔
852
    }
853

854
    blockIndex += 1;
395,730,374✔
855

856
    current += p->info.rows;
395,730,374✔
857
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
395,729,742✔
858
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
859
    void* tmp = taosArrayPush(pResList, &p);
395,732,764✔
860
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
395,732,764✔
861

862
    if (current >= rowsThreshold || processOneBlock) {
395,732,764✔
863
      break;
864
    }
865

866
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
379,228,699✔
867
    QUERY_CHECK_CODE(code, lino, _end);
379,197,501✔
868
    code = blockDataCheck(pRes);
379,197,501✔
869
    QUERY_CHECK_CODE(code, lino, _end);
379,231,381✔
870
  }
871

872
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
174,484,272✔
873
    pTaskInfo->pSubplan->rowsThreshold -= current;
797,008✔
874
  }
875

876
  *hasMore = (pRes != NULL);
174,486,744✔
877
  uint64_t el = (taosGetTimestampUs() - st);
174,472,410✔
878

879
  pTaskInfo->cost.elapsedTime += el;
174,472,410✔
880
  if (NULL == pRes) {
174,480,379✔
881
    *useconds = pTaskInfo->cost.elapsedTime;
157,976,389✔
882
  }
883

884
_end:
174,342,726✔
885
  (void)cleanUpUdfs();
174,475,953✔
886

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

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

897
  return pTaskInfo->code;
174,492,358✔
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,453,971✔
915
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,453,971✔
916
  int64_t        threadId = taosGetSelfPthreadId();
39,453,971✔
917
  int64_t        curOwner = 0;
39,454,187✔
918

919
  *pRes = NULL;
39,454,187✔
920

921
  // todo extract method
922
  taosRLockLatch(&pTaskInfo->lock);
39,454,187✔
923
  bool isKilled = isTaskKilled(pTaskInfo);
39,454,187✔
924
  if (isKilled) {
39,453,860✔
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,453,860✔
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,454,075✔
940
  taosRUnLockLatch(&pTaskInfo->lock);
39,454,075✔
941

942
  if (pTaskInfo->cost.start == 0) {
39,454,303✔
943
    pTaskInfo->cost.start = taosGetTimestampUs();
96,836✔
944
  }
945

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

958
  int64_t st = taosGetTimestampUs();
39,453,739✔
959
  int32_t code = TSDB_CODE_SUCCESS;
39,453,739✔
960
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
39,453,739✔
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,453,403✔
965
  }
966
  if (code) {
39,437,659✔
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,437,659✔
972
  if (code) {
39,453,266✔
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,441,282✔
978

979
  pTaskInfo->cost.elapsedTime += el;
39,441,282✔
980
  if (NULL == *pRes) {
39,450,799✔
981
    *useconds = pTaskInfo->cost.elapsedTime;
3,950,403✔
982
  }
983

984
  (void)cleanUpUdfs();
39,451,274✔
985

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

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

992
  atomic_store_64(&pTaskInfo->owner, 0);
39,454,336✔
993
  return pTaskInfo->code;
39,454,411✔
994
}
995

996
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
49,344,440✔
997
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
49,344,440✔
998
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
49,344,879✔
999
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
49,344,879✔
1000

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

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

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

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

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

1066
  setTaskKilled(pTaskInfo, rspCode);
19,835✔
1067
  qStopTaskOperators(pTaskInfo);
19,835✔
1068

1069
  return TSDB_CODE_SUCCESS;
19,835✔
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) {
153,781,849✔
1126
  STaskCostInfo* pSummary = &pTaskInfo->cost;
153,781,849✔
1127
  int64_t        idleTime = pSummary->start - pSummary->created;
153,782,657✔
1128

1129
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
153,780,365✔
1130
  if (pSummary->pRecoder != NULL) {
153,775,716✔
1131
    qDebug(
111,323,546✔
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,
42,457,159✔
1140
           pSummary->elapsedTime / 1000.0);
1141
  }
1142
}
153,781,364✔
1143

1144
void qDestroyTask(qTaskInfo_t qTaskHandle) {
161,051,949✔
1145
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
161,051,949✔
1146
  if (pTaskInfo == NULL) {
161,051,949✔
1147
    return;
7,275,029✔
1148
  }
1149

1150
  if (pTaskInfo->pRoot != NULL) {
153,776,920✔
1151
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
153,780,497✔
1152
  } else {
1153
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1154
  }
1155

1156
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
153,781,701✔
1157
  doDestroyTask(pTaskInfo);
153,781,389✔
1158
}
1159

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

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

1169
  while (1) {
118,222✔
1170
    uint16_t type = pOperator->operatorType;
239,351✔
1171
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
239,351✔
1172
      *scanner = pOperator->info;
121,129✔
1173
      break;
121,129✔
1174
    } else {
1175
      pOperator = pOperator->pDownstream[0];
118,222✔
1176
    }
1177
  }
1178
}
121,129✔
1179

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

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

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

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

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

1223
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
149,562✔
1224
  pCond->suid = pMtInfo->suid;
149,736✔
1225
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
149,562✔
1226
  pCond->startVersion = -1;
149,968✔
1227
  pCond->endVersion = sContext->snapVersion;
149,968✔
1228

1229
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
954,700✔
1230
    SColumnInfo* pColInfo = &pCond->colList[i];
804,661✔
1231
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
804,835✔
1232
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
804,777✔
1233
    if (pMtInfo->pExtSchemas != NULL) {
804,777✔
1234
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,820✔
1235
    }
1236
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
804,661✔
1237
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
804,500✔
1238

1239
    pCond->pSlotList[i] = i;
804,358✔
1240
  }
1241

1242
  return TSDB_CODE_SUCCESS;
149,968✔
1243
}
1244

1245
void qStreamSetOpen(qTaskInfo_t tinfo) {
39,148,030✔
1246
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
39,148,030✔
1247
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
39,148,030✔
1248
  pOperator->status = OP_NOT_OPENED;
39,151,534✔
1249
}
39,150,453✔
1250

1251
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,252,880✔
1252
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,252,880✔
1253
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,252,880✔
1254
}
4,253,474✔
1255

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

1261
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,475,004✔
1262
  const char*    id = GET_TASKID(pTaskInfo);
4,474,824✔
1263

1264
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,474,466✔
1265
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,187,978✔
1266
    if (pOperator == NULL || code != 0) {
4,187,031✔
UNCOV
1267
      return code;
×
1268
    }
1269

1270
    SStreamScanInfo* pInfo = pOperator->info;
4,187,128✔
1271
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,187,171✔
1272
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,188,010✔
1273
    walReaderVerifyOffset(pWalReader, pOffset);
4,187,942✔
1274
  }
1275
  // if pOffset equal to current offset, means continue consume
1276
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,473,500✔
1277
    return 0;
4,012,634✔
1278
  }
1279

1280
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
461,140✔
1281
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
307,853✔
1282
    if (pOperator == NULL || code != 0) {
307,692✔
UNCOV
1283
      return code;
×
1284
    }
1285

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

1291
    if (pOffset->type == TMQ_OFFSET__LOG) {
307,821✔
1292
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
243,648✔
1293
      pScanBaseInfo->dataReader = NULL;
243,648✔
1294

1295
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
243,648✔
1296
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
243,673✔
1297
      walReaderVerifyOffset(pWalReader, pOffset);
243,648✔
1298
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
243,673✔
1299
      if (code < 0) {
243,673✔
1300
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,716✔
1301
        return code;
7,716✔
1302
      }
1303
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
64,000✔
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;
64,180✔
1307
      int64_t ts = pOffset->ts;
64,180✔
1308
      int32_t index = 0;
64,062✔
1309

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1429
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
149,856✔
1430
      tableListClear(pTableListInfo);
149,949✔
1431

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

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

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

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

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

1479
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
149,839✔
1480
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
149,910✔
1481
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1482
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
149,678✔
1483
      pTaskInfo->streamInfo.schema = mtInfo.schema;
149,678✔
1484
      taosMemoryFreeClear(mtInfo.pExtSchemas);
150,026✔
1485

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

1505
end:
400,756✔
1506
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
401,003✔
1507
  return 0;
401,188✔
1508
}
1509

1510
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
105,658,178✔
1511
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
105,658,178✔
1512
  if (pMsg->info.ahandle == NULL) {
105,668,300✔
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, 
105,645,809✔
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};
105,651,488✔
1521

1522
  if (pMsg->contLen > 0) {
105,664,537✔
1523
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
105,649,421✔
1524
    if (buf.pData == NULL) {
105,641,315✔
1525
      pMsg->code = terrno;
×
1526
    } else {
1527
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
105,641,315✔
1528
    }
1529
  }
1530

1531
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
105,666,223✔
1532
  rpcFreeCont(pMsg->pCont);
105,673,272✔
1533
  destroySendMsgInfo(pSendInfo);
105,648,270✔
1534
}
1535

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

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

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

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

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

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

UNCOV
1564
  taosArrayDestroy(plist);
×
1565

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

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

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

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

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

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

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

1608
  *pList = NULL;
1,680,283✔
1609
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,680,872✔
1610
  if (pArray == NULL) {
1,680,882✔
1611
    return terrno;
×
1612
  }
1613

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

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

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

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

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

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

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

1658
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
43,290,575✔
1659
  int32_t code = 0;
43,290,575✔
1660

1661
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
43,290,575✔
1662
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
43,288,134✔
1663

1664
  if (pOper->fpSet.resetStateFn) {
43,289,310✔
1665
    code = pOper->fpSet.resetStateFn(pOper);
43,290,951✔
1666
  }
1667
  pOper->status = OP_NOT_OPENED;
43,287,334✔
1668
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
73,364,062✔
1669
    code = clearStatesForOperator(pOper->pDownstream[i]);
30,070,230✔
1670
  }
1671
  return code;
43,292,996✔
1672
}
1673

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

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

1688
  *ppRes = NULL;
17,843,202✔
1689

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

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

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

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

1709
  pTaskInfo->owner = threadId;
17,843,202✔
1710
  taosRUnLockLatch(&pTaskInfo->lock);
17,843,694✔
1711

1712
  if (pTaskInfo->cost.start == 0) {
17,843,202✔
1713
    pTaskInfo->cost.start = taosGetTimestampUs();
310,016✔
1714
  }
1715

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

1726
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,843,270✔
1727

1728
  int64_t st = taosGetTimestampUs();
17,843,694✔
1729

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

1743
  uint64_t el = (taosGetTimestampUs() - st);
15,100,173✔
1744

1745
  pTaskInfo->cost.elapsedTime += el;
15,100,173✔
1746
  if (NULL == *ppRes) {
15,100,154✔
1747
    *useconds = pTaskInfo->cost.elapsedTime;
9,780,511✔
1748
  }
1749

1750
  (void)cleanUpUdfs();
15,100,154✔
1751

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

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

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

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

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

1794
end:
309,344✔
1795
  return 0;
309,344✔
1796
}
1797

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2172
  if (pSrc->pFields && pSrc->pFields->size > 0) {
301,200✔
2173
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
300,708✔
2174
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
300,708✔
2175
  } else {
2176
    (*ppDst)->pFields = NULL;
×
2177
  }
2178
  
2179
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
301,200✔
2180
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
201,867✔
2181
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
201,867✔
2182
  } else {
2183
    (*ppDst)->pTagFields = NULL;
99,333✔
2184
  }
2185

2186
_exit:
301,200✔
2187

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

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

UNCOV
2203
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
UNCOV
2204
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2205
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc