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

taosdata / TDengine / #4927

14 Jan 2026 07:42AM UTC coverage: 66.12% (+0.07%) from 66.053%
#4927

push

travis-ci

web-flow
fix memleak decimal (#34283)

2 of 16 new or added lines in 1 file covered. (12.5%)

476 existing lines in 124 files now uncovered.

200574 of 303348 relevant lines covered (66.12%)

130242324.59 hits per line

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

69.09
/source/libs/executor/src/executor.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "taosdef.h"
32
#include "tarray.h"
33
#include "tdatablock.h"
34
#include "tref.h"
35
#include "trpc.h"
36
#include "tudf.h"
37
#include "wal.h"
38

39
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
40
int32_t             exchangeObjRefPool = -1;
41
SGlobalExecInfo     gExecInfo = {0};
42

43
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
918,493,147✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
918,493,147✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
918,493,147✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
918,721,791✔
47
}
918,284,274✔
48

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

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

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

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

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
45,550,789✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
45,550,789✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
45,550,984✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
20,816,169✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
20,816,169✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
20,816,509✔
162
      if (pScanInfo->base.dataReader != NULL) {
20,814,908✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
262,233✔
164
        if (code) {
261,928✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
24,733,824✔
172
  }
173

174
  return 0;
24,236,791✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
24,237,207✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
24,237,207✔
179
  pTaskInfo->id.queryId = queryId;
24,237,207✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
24,237,816✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
24,237,792✔
184
}
185

186
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
187
  SExecTaskInfo* pTaskInfo = tinfo;
×
188
  return pTaskInfo->status == OP_EXEC_DONE;
×
189
}
190

191
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
192
  if (tinfo == NULL) {
×
193
    return TSDB_CODE_APP_ERROR;
×
194
  }
195

196
  if (pBlocks == NULL || numOfBlocks == 0) {
×
197
    return TSDB_CODE_SUCCESS;
×
198
  }
199

200
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
201

202
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
203
  if (code != TSDB_CODE_SUCCESS) {
×
204
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
205
  } else {
206
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
207
  }
208

209
  return code;
×
210
}
211

212
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
434,731✔
213
  if (msg == NULL) {  // create raw scan
434,731✔
214
    SExecTaskInfo* pTaskInfo = NULL;
115,175✔
215

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

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

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

232
  SSubplan* pPlan = NULL;
319,556✔
233
  int32_t   code = qStringToSubplan(msg, &pPlan);
319,556✔
234
  if (code != TSDB_CODE_SUCCESS) {
319,556✔
235
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
236
    terrno = code;
×
237
    return NULL;
×
238
  }
239

240
  qTaskInfo_t pTaskInfo = NULL;
319,556✔
241
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
319,556✔
242
  if (code != TSDB_CODE_SUCCESS) {
319,865✔
243
    qDestroyTask(pTaskInfo);
×
244
    terrno = code;
×
245
    return NULL;
×
246
  }
247

248
  return pTaskInfo;
319,865✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
510,040✔
252
  if (streamInserterParam == NULL) {
510,040✔
253
    return TSDB_CODE_SUCCESS;
297,968✔
254
  }
255

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

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

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

272
  return TSDB_CODE_SUCCESS;
212,072✔
273
}
274

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

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

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

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

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

330
_error:
69,830✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,911,376✔
342
  if (pInfo == NULL) {
2,911,376✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,911,376✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,911,376✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,911,597✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,911,597✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,711,011✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
438,325✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,622,608✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

362
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,473,272✔
363
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,473,272✔
364
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,473,272✔
365
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,472,816✔
366
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,473,037✔
367
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,472,816✔
368
}
369

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,911,597✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
200,586✔
382
    setReadHandle(handle, pScanBaseInfo);
200,586✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,711,011✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,272,686✔
385
    setReadHandle(handle, pScanBaseInfo);
2,272,686✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
510,040✔
400

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

416
  return code;
509,829✔
417
}
418

419
typedef struct {
420
  tb_uid_t tableUid;
421
  tb_uid_t childUid;
422
  int8_t   check;
423
} STqPair;
424

425
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
362,001✔
426
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
427
  int32_t code = TSDB_CODE_SUCCESS;
362,001✔
428
  int32_t lino = 0;
362,001✔
429
  int8_t  locked = 0;
362,001✔
430
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
362,001✔
431

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
362,001✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
362,001✔
444

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

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

454
  locked = 1;
362,001✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
738,357✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
376,356✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
376,356✔
458

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

465
    tDecoderClear(&mr.coder);
376,356✔
466

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

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
362,001✔
524

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

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

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

543
  if (isAdd) {
364,018✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
362,001✔
545
  }
546

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

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

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

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

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

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

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

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

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

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

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

638
  return code;
364,018✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
713,365,178✔
647
    return TSDB_CODE_INVALID_PARA;
65,601✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
713,299,603✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
713,299,603✔
652
    return TSDB_CODE_SUCCESS;
409,693,210✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
303,773,496✔
662
  *tversion = pSchemaInfo->tversion;
303,893,871✔
663
  *rversion = pSchemaInfo->rversion;
303,980,594✔
664
  if (pSchemaInfo->dbname) {
303,708,647✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
303,578,454✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
303,880,644✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
303,782,796✔
671
  } else {
UNCOV
672
    tableName[0] = 0;
×
673
  }
674

675
  *tbGet = true;
303,927,687✔
676

677
  return TSDB_CODE_SUCCESS;
303,797,566✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
409,516,016✔
681

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

689
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
75,785,211✔
690
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
75,785,211✔
691
  ((SExecTaskInfo*)tinfo)->paramSet = false;
75,787,233✔
692
}
75,788,773✔
693

694
int32_t qExecutorInit(void) {
4,228,456✔
695
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,228,456✔
696
  return TSDB_CODE_SUCCESS;
4,229,218✔
697
}
698

699
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
73,219,454✔
700
  int32_t        code = TSDB_CODE_SUCCESS;
73,219,454✔
701
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
73,219,454✔
702
  if (pTask->pWorkerCb) {
73,219,454✔
703
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
73,222,171✔
704
    if (code != TSDB_CODE_SUCCESS) {
73,244,142✔
705
      pTask->code = code;
×
706
      return pTask->code;
×
707
    }
708
  }
709

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

717
  if (pTask->pWorkerCb) {
73,244,554✔
718
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
73,246,814✔
719
    if (code != TSDB_CODE_SUCCESS) {
73,248,170✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
73,246,814✔
725
}
726

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

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

736
  readHandle->uid = 0;
423,973,002✔
737
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
423,993,864✔
738
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
423,764,764✔
739
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
12,721,976✔
740
    goto _error;
12,315,153✔
741
  }
742
    
743
  if (handle) {
411,146,230✔
744
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
411,072,952✔
745
    void*           pSinkManager = NULL;
411,202,007✔
746
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
410,343,074✔
747
    if (code != TSDB_CODE_SUCCESS) {
411,070,259✔
748
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
749
      goto _error;
×
750
    }
751

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

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

771
    // pSinkParam has been freed during create sinker.
772
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
410,921,787✔
773
                              (*pTask)->id.str, pSubplan->processOneBlock);
411,242,632✔
774
    if (code) {
410,793,472✔
775
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
583✔
776
    }
777
  }
778

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

782
_error:
211,762,326✔
783
  // if failed to add ref for all tables in this query, abort current query
784
  return code;
423,779,070✔
785
}
786

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

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

799
  if (pLocal) {
497,371,277✔
800
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
494,196,059✔
801
  }
802

803
  taosArrayClear(pResList);
496,971,664✔
804

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

812
  if (pTaskInfo->cost.start == 0) {
497,189,072✔
813
    pTaskInfo->cost.start = taosGetTimestampUs();
405,901,033✔
814
  }
815

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

822
  // error occurs, record the error code and return to client
823
  int32_t ret = setjmp(pTaskInfo->env);
497,158,884✔
824
  if (ret != TSDB_CODE_SUCCESS) {
508,430,909✔
825
    pTaskInfo->code = ret;
11,792,489✔
826
    (void)cleanUpUdfs();
11,792,037✔
827

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

831
    return pTaskInfo->code;
11,792,489✔
832
  }
833

834
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
496,638,420✔
835

836
  int32_t      current = 0;
496,640,098✔
837
  SSDataBlock* pRes = NULL;
496,640,098✔
838
  int64_t      st = taosGetTimestampUs();
497,621,781✔
839

840
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
497,621,781✔
841
    pTaskInfo->paramSet = true;
75,786,559✔
842
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
75,786,867✔
843
  } else {
844
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
421,740,575✔
845
  }
846

847
  QUERY_CHECK_CODE(code, lino, _end);
485,874,125✔
848
  code = blockDataCheck(pRes);
485,874,125✔
849
  QUERY_CHECK_CODE(code, lino, _end);
485,953,748✔
850

851
  if (pRes == NULL) {
485,953,748✔
852
    st = taosGetTimestampUs();
100,409,233✔
853
  }
854

855
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
485,965,593✔
856
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
485,885,491✔
857
    rowsThreshold = 4096;
485,380,022✔
858
  }
859

860
  int32_t blockIndex = 0;
485,919,550✔
861
  while (pRes != NULL) {
1,134,722,736✔
862
    SSDataBlock* p = NULL;
690,389,783✔
863
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
690,321,129✔
864
      SSDataBlock* p1 = NULL;
540,925,909✔
865
      code = createOneDataBlock(pRes, true, &p1);
540,933,446✔
866
      QUERY_CHECK_CODE(code, lino, _end);
540,963,011✔
867

868
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
540,963,011✔
869
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
541,037,313✔
870
      p = p1;
541,037,313✔
871
    } else {
872
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
149,480,994✔
873
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
149,480,326✔
874

875
      p = *(SSDataBlock**)tmp;
149,480,326✔
876
      code = copyDataBlock(p, pRes);
149,480,326✔
877
      QUERY_CHECK_CODE(code, lino, _end);
149,481,984✔
878
    }
879

880
    blockIndex += 1;
690,520,311✔
881

882
    current += p->info.rows;
690,520,311✔
883
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
690,511,726✔
884
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
885
    void* tmp = taosArrayPush(pResList, &p);
690,543,013✔
886
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
690,543,013✔
887

888
    if (current >= rowsThreshold || processOneBlock) {
690,543,013✔
889
      break;
890
    }
891

892
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
648,914,341✔
893
    QUERY_CHECK_CODE(code, lino, _end);
648,841,057✔
894
    code = blockDataCheck(pRes);
648,841,057✔
895
    QUERY_CHECK_CODE(code, lino, _end);
648,882,030✔
896
  }
897

898
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
485,961,625✔
899
    pTaskInfo->pSubplan->rowsThreshold -= current;
510,327✔
900
  }
901

902
  *hasMore = (pRes != NULL);
485,982,659✔
903
  uint64_t el = (taosGetTimestampUs() - st);
485,912,053✔
904

905
  pTaskInfo->cost.elapsedTime += el;
485,912,053✔
906
  if (NULL == pRes) {
485,924,127✔
907
    *useconds = pTaskInfo->cost.elapsedTime;
444,295,455✔
908
  }
909

910
_end:
485,894,361✔
911
  (void)cleanUpUdfs();
485,877,417✔
912

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

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

923
  return pTaskInfo->code;
486,030,939✔
924
}
925

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

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

940
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
123,300,533✔
941
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
123,300,533✔
942
  int64_t        threadId = taosGetSelfPthreadId();
123,300,533✔
943
  int64_t        curOwner = 0;
123,299,519✔
944

945
  *pRes = NULL;
123,299,519✔
946

947
  // todo extract method
948
  taosRLockLatch(&pTaskInfo->lock);
123,299,857✔
949
  bool isKilled = isTaskKilled(pTaskInfo);
123,301,546✔
950
  if (isKilled) {
123,301,236✔
951
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
952

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

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

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

965
  pTaskInfo->owner = threadId;
123,300,562✔
966
  taosRUnLockLatch(&pTaskInfo->lock);
123,301,236✔
967

968
  if (pTaskInfo->cost.start == 0) {
123,300,909✔
969
    pTaskInfo->cost.start = taosGetTimestampUs();
225,057✔
970
  }
971

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

982
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
123,299,518✔
983

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

997
  code = blockDataCheck(*pRes);
123,249,573✔
998
  if (code) {
123,295,971✔
999
    pTaskInfo->code = code;
×
1000
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1001
  }
1002

1003
  uint64_t el = (taosGetTimestampUs() - st);
123,254,049✔
1004

1005
  pTaskInfo->cost.elapsedTime += el;
123,254,049✔
1006
  if (NULL == *pRes) {
123,288,719✔
1007
    *useconds = pTaskInfo->cost.elapsedTime;
20,314,099✔
1008
  }
1009

1010
  (void)cleanUpUdfs();
123,288,413✔
1011

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

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

1018
  atomic_store_64(&pTaskInfo->owner, 0);
123,300,870✔
1019
  return pTaskInfo->code;
123,301,208✔
1020
}
1021

1022
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
124,923,942✔
1023
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
124,923,942✔
1024
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
124,924,846✔
1025
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
124,924,885✔
1026

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

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

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

1044
  return 0;
×
1045
}
1046

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

1056
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
482,620✔
1057
  if (pTaskInfo->subJobCtx.hasSubJobs) {
482,620✔
1058
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
438,142✔
1059
    if (pTaskInfo->subJobCtx.param) {
438,142✔
1060
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
224,579✔
1061
    }
1062
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
438,142✔
1063
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
438,142✔
1064
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
438,142✔
1065
  }
1066
  
1067
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
482,620✔
1068

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

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

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

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

1102
  setTaskKilled(pTaskInfo, rspCode);
482,620✔
1103
  qStopTaskOperators(pTaskInfo);
482,620✔
1104

1105
  return TSDB_CODE_SUCCESS;
482,620✔
1106
}
1107

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

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

1121
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1122

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

1129
        taosMsleep(200);
×
1130

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

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

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

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

1161
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
412,380,695✔
1162
  STaskCostInfo* pSummary = &pTaskInfo->cost;
412,380,695✔
1163
  int64_t        idleTime = pSummary->start - pSummary->created;
412,425,046✔
1164

1165
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
412,380,697✔
1166
  if (pSummary->pRecoder != NULL) {
412,386,499✔
1167
    qDebug(
298,139,552✔
1168
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1169
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1170
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1171
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1172
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1173
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1174
  } else {
1175
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
114,271,396✔
1176
           pSummary->elapsedTime / 1000.0);
1177
  }
1178
}
412,410,583✔
1179

1180

1181
void qDestroyTask(qTaskInfo_t qTaskHandle) {
430,193,392✔
1182
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
430,193,392✔
1183
  if (pTaskInfo == NULL) {
430,193,392✔
1184
    return;
17,817,124✔
1185
  }
1186

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

1193
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
412,413,041✔
1194
  doDestroyTask(pTaskInfo);
412,363,183✔
1195
}
1196

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

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

1206
  while (1) {
313,917✔
1207
    uint16_t type = pOperator->operatorType;
633,782✔
1208
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
633,782✔
1209
      *scanner = pOperator->info;
319,865✔
1210
      break;
319,865✔
1211
    } else {
1212
      pOperator = pOperator->pDownstream[0];
313,917✔
1213
    }
1214
  }
1215
}
319,865✔
1216

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

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

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

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

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

1248
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
750,700✔
1249
  memset(pCond, 0, sizeof(SQueryTableDataCond));
750,700✔
1250
  pCond->order = TSDB_ORDER_ASC;
750,700✔
1251
  pCond->numOfCols = pMtInfo->schema->nCols;
751,597✔
1252
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
751,298✔
1253
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
750,437✔
1254
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
751,035✔
1255
    taosMemoryFreeClear(pCond->colList);
×
1256
    taosMemoryFreeClear(pCond->pSlotList);
×
1257
    return terrno;
×
1258
  }
1259

1260
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
751,035✔
1261
  pCond->suid = pMtInfo->suid;
751,298✔
1262
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
750,700✔
1263
  pCond->startVersion = -1;
751,298✔
1264
  pCond->endVersion = sContext->snapVersion;
751,035✔
1265

1266
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,744,541✔
1267
    SColumnInfo* pColInfo = &pCond->colList[i];
3,993,542✔
1268
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,991,748✔
1269
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,992,346✔
1270
    if (pMtInfo->pExtSchemas != NULL) {
3,992,980✔
1271
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,737✔
1272
    }
1273
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,993,542✔
1274
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,992,047✔
1275

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

1279
  return TSDB_CODE_SUCCESS;
751,298✔
1280
}
1281

1282
void qStreamSetOpen(qTaskInfo_t tinfo) {
121,771,302✔
1283
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
121,771,302✔
1284
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
121,771,302✔
1285
  pOperator->status = OP_NOT_OPENED;
121,789,414✔
1286
}
121,786,121✔
1287

1288
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
20,618,484✔
1289
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,618,484✔
1290
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
20,618,484✔
1291
}
20,620,813✔
1292

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

1298
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
21,618,203✔
1299
  const char*    id = GET_TASKID(pTaskInfo);
21,617,233✔
1300

1301
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
21,614,898✔
1302
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
20,323,193✔
1303
    if (pOperator == NULL || code != 0) {
20,324,543✔
1304
      return code;
388✔
1305
    }
1306

1307
    SStreamScanInfo* pInfo = pOperator->info;
20,324,155✔
1308
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
20,322,852✔
1309
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
20,323,052✔
1310
    walReaderVerifyOffset(pWalReader, pOffset);
20,323,038✔
1311
  }
1312
  // if pOffset equal to current offset, means continue consume
1313
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
21,613,717✔
1314
    return 0;
19,762,505✔
1315
  }
1316

1317
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,851,656✔
1318
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,084,828✔
1319
    if (pOperator == NULL || code != 0) {
1,084,506✔
1320
      return code;
×
1321
    }
1322

1323
    SStreamScanInfo* pInfo = pOperator->info;
1,084,775✔
1324
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,085,526✔
1325
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,084,842✔
1326
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,084,498✔
1327

1328
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,085,172✔
1329
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
855,735✔
1330
      pScanBaseInfo->dataReader = NULL;
855,422✔
1331

1332
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
855,084✔
1333
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
854,699✔
1334
      walReaderVerifyOffset(pWalReader, pOffset);
854,699✔
1335
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
856,372✔
1336
      if (code < 0) {
856,372✔
1337
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
8,774✔
1338
        return code;
8,774✔
1339
      }
1340
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
229,098✔
1341
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1342
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1343
      int64_t uid = pOffset->uid;
229,715✔
1344
      int64_t ts = pOffset->ts;
229,091✔
1345
      int32_t index = 0;
229,403✔
1346

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

1357
      if (uid == 0) {
229,403✔
1358
        if (numOfTables != 0) {
224,593✔
1359
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
39,308✔
1360
          if (!tmp) {
39,020✔
1361
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1362
            taosRUnLockLatch(&pTaskInfo->lock);
×
1363
            return terrno;
×
1364
          }
1365
          if (tmp) uid = tmp->uid;
39,020✔
1366
          ts = INT64_MIN;
39,020✔
1367
          pScanInfo->currentTable = 0;
39,020✔
1368
        } else {
1369
          taosRUnLockLatch(&pTaskInfo->lock);
185,285✔
1370
          qError("no table in table list, %s", id);
184,980✔
1371
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
185,597✔
1372
        }
1373
      }
1374
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
43,830✔
1375

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1466
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
751,456✔
1467
      tableListClear(pTableListInfo);
751,500✔
1468

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

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

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

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

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

1516
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
749,498✔
1517
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
750,401✔
1518
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1519
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
749,498✔
1520
      pTaskInfo->streamInfo.schema = mtInfo.schema;
749,803✔
1521
      taosMemoryFreeClear(mtInfo.pExtSchemas);
750,999✔
1522

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

1542
end:
1,658,436✔
1543
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,658,006✔
1544
  return 0;
1,658,544✔
1545
}
1546

1547
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
369,016,611✔
1548
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
369,016,611✔
1549
  if (pMsg->info.ahandle == NULL) {
369,134,826✔
1550
    rpcFreeCont(pMsg->pCont);
×
1551
    qError("pMsg->info.ahandle is NULL");
×
1552
    return;
×
1553
  }
1554

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

1558
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
368,998,354✔
1559

1560
  if (pMsg->contLen > 0) {
369,040,719✔
1561
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
369,031,752✔
1562
    if (buf.pData == NULL) {
368,797,984✔
1563
      pMsg->code = terrno;
×
1564
    } else {
1565
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
368,797,984✔
1566
    }
1567
  }
1568

1569
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
369,049,776✔
1570
  rpcFreeCont(pMsg->pCont);
369,172,437✔
1571
  destroySendMsgInfo(pSendInfo);
369,098,940✔
1572
}
1573

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

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

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

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

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

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

1602
  taosArrayDestroy(plist);
×
1603

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

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

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

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

1626
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,675,913✔
1627
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,675,373✔
1628
  } else {
1629
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,675,913✔
1630
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,676,453✔
1631
    }
1632
  }
1633

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

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

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

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

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

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

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

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

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

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

1696
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
30,461,050✔
1697
  int32_t code = 0;
30,461,050✔
1698

1699
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
30,461,050✔
1700
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
30,459,525✔
1701

1702
  if (pOper->fpSet.resetStateFn) {
30,458,442✔
1703
    code = pOper->fpSet.resetStateFn(pOper);
30,459,967✔
1704
  }
1705
  pOper->status = OP_NOT_OPENED;
30,456,306✔
1706
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
52,664,974✔
1707
    code = clearStatesForOperator(pOper->pDownstream[i]);
22,201,291✔
1708
  }
1709
  return code;
30,464,002✔
1710
}
1711

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

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

1726
  *ppRes = NULL;
11,040,638✔
1727

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

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

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

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

1747
  pTaskInfo->owner = threadId;
11,040,203✔
1748
  taosRUnLockLatch(&pTaskInfo->lock);
11,040,424✔
1749

1750
  if (pTaskInfo->cost.start == 0) {
11,040,417✔
1751
    pTaskInfo->cost.start = taosGetTimestampUs();
218,559✔
1752
  }
1753

1754
  // error occurs, record the error code and return to client
1755
  int32_t ret = setjmp(pTaskInfo->env);
11,040,417✔
1756
  if (ret != TSDB_CODE_SUCCESS) {
13,479,547✔
1757
    pTaskInfo->code = ret;
2,439,860✔
1758
    (void)cleanUpUdfs();
2,439,860✔
1759
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,439,860✔
1760
    atomic_store_64(&pTaskInfo->owner, 0);
2,439,860✔
1761
    return pTaskInfo->code;
2,439,860✔
1762
  }
1763

1764
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
11,039,687✔
1765

1766
  int64_t st = taosGetTimestampUs();
11,040,424✔
1767

1768
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,040,424✔
1769
  if (code) {
8,600,778✔
1770
    pTaskInfo->code = code;
×
1771
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1772
  } else {
1773
    *finished = *ppRes == NULL;
8,600,778✔
1774
    code = blockDataCheck(*ppRes);
8,600,543✔
1775
  }
1776
  if (code) {
8,600,543✔
1777
    pTaskInfo->code = code;
×
1778
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1779
  }
1780

1781
  uint64_t el = (taosGetTimestampUs() - st);
8,600,543✔
1782

1783
  pTaskInfo->cost.elapsedTime += el;
8,600,543✔
1784
  if (NULL == *ppRes) {
8,600,543✔
1785
    *useconds = pTaskInfo->cost.elapsedTime;
5,363,837✔
1786
  }
1787

1788
  (void)cleanUpUdfs();
8,600,567✔
1789

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

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

1796
  atomic_store_64(&pTaskInfo->owner, 0);
8,600,778✔
1797
  return pTaskInfo->code;
8,600,778✔
1798
}
1799

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

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

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

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

1832
end:
233,850✔
1833
  return 0;
233,850✔
1834
}
1835

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2149
    memcpy(tbname, pVal, len);
230,898✔
2150
    tbname[len] = '\0';  // ensure null terminated
230,898✔
2151
    if (NULL != strchr(tbname, '.')) {
231,013✔
2152
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2153
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2154
    }
2155
  }
2156
  // TODO free dst
2157
  sclFreeParam(&dst);
233,832✔
2158
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
233,669✔
2159
  return code;
234,069✔
2160
}
2161

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

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

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

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

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

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

2220
  if (pSrc->pFields && pSrc->pFields->size > 0) {
212,072✔
2221
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
212,072✔
2222
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
212,072✔
2223
  } else {
UNCOV
2224
    (*ppDst)->pFields = NULL;
×
2225
  }
2226
  
2227
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
212,072✔
2228
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
132,838✔
2229
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
132,838✔
2230
  } else {
2231
    (*ppDst)->pTagFields = NULL;
79,234✔
2232
  }
2233

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

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

2248
_exit:
211,861✔
2249

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

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

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

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

2282
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,426✔
2283
  pList->idInfo.suid = suid;
4,426✔
2284
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,426✔
2285
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,426✔
2286
  if (code != TSDB_CODE_SUCCESS) {
4,426✔
2287
    goto end;
×
2288
  }
2289
  // taosArrayClear(uidList);
2290
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2291
  //   void* tmp = taosArrayGet(uidListCopy, i);
2292
  //   if (tmp == NULL) {
2293
  //     continue;
2294
  //   }
2295
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2296
  //   if (slot == NULL) {
2297
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2298
  //       code = terrno;
2299
  //       goto end;
2300
  //     }
2301
  //   }
2302
  // }
2303
end:
4,426✔
2304
  // taosArrayDestroy(uidListCopy);
2305
  tableListDestroy(pList);
4,426✔
2306
  return code;
4,426✔
2307
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc