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

taosdata / TDengine / #4747

21 Sep 2025 11:53PM UTC coverage: 58.002% (-1.1%) from 59.065%
#4747

push

travis-ci

web-flow
fix: refine python taos error log matching in checkAsan.sh (#33029)

* fix: refine python taos error log matching in checkAsan.sh

* fix: improve python taos error log matching in checkAsan.sh

133398 of 293157 branches covered (45.5%)

Branch coverage included in aggregate %.

201778 of 284713 relevant lines covered (70.87%)

5539418.83 hits per line

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

53.83
/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 "executorInt.h"
21
#include "libs/new-stream/stream.h"
22
#include "operator.h"
23
#include "osMemPool.h"
24
#include "osMemory.h"
25
#include "planner.h"
26
#include "query.h"
27
#include "querytask.h"
28
#include "storageapi.h"
29
#include "streamexecutorInt.h"
30
#include "tdatablock.h"
31
#include "tref.h"
32
#include "trpc.h"
33
#include "tudf.h"
34
#include "wal.h"
35

36
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
37
int32_t             exchangeObjRefPool = -1;
38
SGlobalExecInfo     gExecInfo = {0};
39

40
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
2,387✔
41
  gExecInfo.dnode = pDnode;
2,387✔
42
  gExecInfo.getMnode = getMnode;
2,387✔
43
  gExecInfo.getDnodeId = getDnodeId;
2,387✔
44
  return;
2,387✔
45
}
46

47
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
115✔
48
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
115!
49
    qError("gExecInfo is not initialized");
×
50
    return TSDB_CODE_APP_ERROR;
×
51
  }
52
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
115✔
53
  return TSDB_CODE_SUCCESS;
115✔
54
}
55

56
static void cleanupRefPool() {
2,280✔
57
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
2,280✔
58
  taosCloseRef(ref);
2,280✔
59
}
2,280✔
60

61
static void initRefPool() {
2,280✔
62
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
2,280✔
63
  (void)atexit(cleanupRefPool);
2,280✔
64
}
2,280✔
65

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

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

84
    SStreamScanInfo* pInfo = pOperator->info;
×
85

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

119
    return TSDB_CODE_SUCCESS;
×
120
  }
121

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

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

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

141
    pOperator->status = OP_NOT_OPENED;
×
142
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
143
  }
144
  return 0;
×
145
}
146

147
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
57,765✔
148
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
57,765✔
149
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
57,765✔
150
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
19,933✔
151
    if (pStreamScanInfo->pTableScanOp != NULL) {
19,933✔
152
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
19,932✔
153
      if (pScanInfo->base.dataReader != NULL) {
19,932✔
154
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
736✔
155
        if (code) {
736!
156
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
157
          return code;
×
158
        }
159
      }
160
    }
161
  } else {
162
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
37,832✔
163
  }
164

165
  return 0;
36,053✔
166
}
167

168
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
36,053✔
169
  SExecTaskInfo* pTaskInfo = tinfo;
36,053✔
170
  pTaskInfo->id.queryId = queryId;
36,053✔
171
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
36,053✔
172

173
  // set the idstr for tsdbReader
174
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
36,053✔
175
}
176

177
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
178
  SExecTaskInfo* pTaskInfo = tinfo;
×
179
  return pTaskInfo->status == OP_EXEC_DONE;
×
180
}
181

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

187
  if (pBlocks == NULL || numOfBlocks == 0) {
×
188
    return TSDB_CODE_SUCCESS;
×
189
  }
190

191
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
192

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

200
  return code;
×
201
}
202

203
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
1,306✔
204
                                     uint64_t id) {
205
  if (msg == NULL) {  // create raw scan
1,306✔
206
    SExecTaskInfo* pTaskInfo = NULL;
290✔
207

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

213
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
290✔
214
    if (NULL == pTaskInfo->pRoot || code != 0) {
290!
215
      taosMemoryFree(pTaskInfo);
×
216
      return NULL;
×
217
    }
218

219
    pTaskInfo->storageAPI = pReaderHandle->api;
290✔
220
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
290!
221
    return pTaskInfo;
290✔
222
  }
223

224
  SSubplan* pPlan = NULL;
1,016✔
225
  int32_t   code = qStringToSubplan(msg, &pPlan);
1,016✔
226
  if (code != TSDB_CODE_SUCCESS) {
1,015!
227
    terrno = code;
×
228
    return NULL;
×
229
  }
230

231
  qTaskInfo_t pTaskInfo = NULL;
1,015✔
232
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE);
1,015✔
233
  if (code != TSDB_CODE_SUCCESS) {
1,017!
234
    qDestroyTask(pTaskInfo);
×
235
    terrno = code;
×
236
    return NULL;
×
237
  }
238

239
  // extract the number of output columns
240
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
1,017✔
241
  *numOfCols = 0;
1,017✔
242

243
  SNode* pNode;
244
  FOREACH(pNode, pDescNode->pSlots) {
8,920!
245
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
7,903✔
246
    if (pSlotDesc->output) {
7,903✔
247
      ++(*numOfCols);
7,902✔
248
    }
249
  }
250

251
  return pTaskInfo;
1,017✔
252
}
253

254
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
16,123✔
255
  if (streamInserterParam == NULL) {
16,123✔
256
    return TSDB_CODE_SUCCESS;
15,209✔
257
  }
258

259
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
914!
260
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
261
    return TSDB_CODE_INVALID_PARA;
×
262
  }
263

264
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
914!
265
    stError("insertParam: invalid db/table name");
×
266
    return TSDB_CODE_INVALID_PARA;
×
267
  }
268

269
  if (streamInserterParam->suid <= 0 &&
914✔
270
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
340!
271
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
272
    return TSDB_CODE_INVALID_PARA;
×
273
  }
274

275
  return TSDB_CODE_SUCCESS;
914✔
276
}
277

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

298
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
16,123✔
299
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
16,123!
300
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
3!
301
    goto _error;
3✔
302
  }
303

304
  if (streamInserterParam) {
16,120✔
305
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
914✔
306
    void*           pSinkManager = NULL;
914✔
307
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
914✔
308
    if (code != TSDB_CODE_SUCCESS) {
914!
309
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
310
      goto _error;
×
311
    }
312

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

325
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
914✔
326
    if (code) {
914!
327
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
328
    }
329
  }
330
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
16,120✔
331
         tstrerror(code));
332

333
_error:
534✔
334

335
  if (code != TSDB_CODE_SUCCESS) {
16,123✔
336
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
3!
337
    if (pInserterParam != NULL) {
3!
338
      taosMemoryFree(pInserterParam);
×
339
    }
340
  }
341
  return code;
16,123✔
342
}
343

344
int32_t qResetTableScan(qTaskInfo_t* pInfo, STimeWindow range) {
×
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
×
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
×
347
  STableScanInfo* pScanInfo = pOperator->info;
×
348
  STableScanBase* pScanBaseInfo = &pScanInfo->base;
×
349

350
  if (range.skey != 0 && range.ekey != 0) {
×
351
    pScanBaseInfo->cond.twindows = range;
×
352
  }
353
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
×
354
  qStreamSetOpen(pTaskInfo);
×
355
  return pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
×
356
}
357

358
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
16,123✔
359
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
360
  if (msg == NULL) {
16,123!
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363

364
  *pTaskInfo = NULL;
16,123✔
365

366
  SSubplan* pPlan = NULL;
16,123✔
367
  int32_t   code = qStringToSubplan(msg, &pPlan);
16,123✔
368
  if (code != TSDB_CODE_SUCCESS) {
16,123!
369
    nodesDestroyNode((SNode *)pPlan);
×
370
    return code;
×
371
  }
372
  // todo: add stream inserter param
373
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
16,123✔
374
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
375
                               pInserterParams);
376
  if (code != TSDB_CODE_SUCCESS) {
16,123✔
377
    qDestroyTask(*pTaskInfo);
3✔
378
    return code;
3✔
379
  }
380

381
  return code;
16,120✔
382
}
383

384
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
231✔
385
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
386
  int32_t code = TSDB_CODE_SUCCESS;
231✔
387
  int32_t lino = 0;
231✔
388
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
231✔
389
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
231!
390
  int32_t numOfUids = taosArrayGetSize(tableIdList);
231✔
391
  if (numOfUids == 0) {
231!
392
    (*ppArrayRes) = qa;
×
393
    goto _error;
×
394
  }
395

396
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
231✔
397

398
  uint64_t suid = 0;
231✔
399
  uint64_t uid = 0;
231✔
400
  int32_t  type = 0;
231✔
401
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
231✔
402

403
  // let's discard the tables those are not created according to the queried super table.
404
  SMetaReader mr = {0};
231✔
405
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
231✔
406
  for (int32_t i = 0; i < numOfUids; ++i) {
516✔
407
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
285✔
408
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
285!
409

410
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
285✔
411
    if (code != TSDB_CODE_SUCCESS) {
285!
412
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
413
      continue;
×
414
    }
415

416
    tDecoderClear(&mr.coder);
285✔
417

418
    if (mr.me.type == TSDB_SUPER_TABLE) {
285!
419
      continue;
×
420
    } else {
421
      if (type == TSDB_SUPER_TABLE) {
285!
422
        // this new created child table does not belong to the scanned super table.
423
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
285!
424
          continue;
×
425
        }
426
      } else {  // ordinary table
427
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
428
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
429
        if (mr.me.uid != uid) {
×
430
          continue;
×
431
        }
432
      }
433
    }
434

435
    if (pScanInfo->pTagCond != NULL) {
285✔
436
      bool          qualified = false;
116✔
437
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
116✔
438
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
116✔
439
      if (code != TSDB_CODE_SUCCESS) {
116!
440
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
441
        continue;
58✔
442
      }
443

444
      if (!qualified) {
116✔
445
        continue;
58✔
446
      }
447
    }
448

449
    // handle multiple partition
450
    void* tmp = taosArrayPush(qa, id);
227✔
451
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
227!
452
  }
453

454
_end:
231✔
455

456
  pAPI->metaReaderFn.clearReader(&mr);
231✔
457
  (*ppArrayRes) = qa;
231✔
458

459
_error:
231✔
460
  if (code != TSDB_CODE_SUCCESS) {
231!
461
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
462
  }
463
  return code;
231✔
464
}
465

466
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
238✔
467
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
238✔
468
  const char*    id = GET_TASKID(pTaskInfo);
238✔
469
  int32_t        code = 0;
238✔
470

471
  if (isAdd) {
238✔
472
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
231!
473
  }
474

475
  // traverse to the stream scanner node to add this table id
476
  SOperatorInfo* pInfo = NULL;
238✔
477
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
238✔
478
  if (code != 0 || pInfo == NULL) {
238!
479
    return code;
×
480
  }
481

482
  SStreamScanInfo* pScanInfo = pInfo->info;
238✔
483
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
238!
484
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
526✔
485
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
288✔
486
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
288✔
487
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
288✔
488
    }
489
  }
490

491
  if (isAdd) {  // add new table id
238✔
492
    SArray* qa = NULL;
231✔
493
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
231✔
494
    if (code != TSDB_CODE_SUCCESS) {
231!
495
      taosArrayDestroy(qa);
×
496
      return code;
×
497
    }
498
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
231✔
499
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
231!
500
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
231✔
501

502
    bool   assignUid = false;
231✔
503
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
231!
504
    char*  keyBuf = NULL;
231✔
505
    if (bufLen > 0) {
231!
506
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
507
      keyBuf = taosMemoryMalloc(bufLen);
×
508
      if (keyBuf == NULL) {
×
509
        taosArrayDestroy(qa);
×
510
        return terrno;
×
511
      }
512
    }
513

514
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
231✔
515
    taosWLockLatch(&pTaskInfo->lock);
231✔
516

517
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
458✔
518
      uint64_t* uid = taosArrayGet(qa, i);
227✔
519
      if (!uid) {
227!
520
        taosMemoryFree(keyBuf);
×
521
        taosArrayDestroy(qa);
×
522
        taosWUnLockLatch(&pTaskInfo->lock);
×
523
        return terrno;
×
524
      }
525
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
227✔
526

527
      if (bufLen > 0) {
227!
528
        if (assignUid) {
×
529
          keyInfo.groupId = keyInfo.uid;
×
530
        } else {
531
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
532
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
533
          if (code != TSDB_CODE_SUCCESS) {
×
534
            taosMemoryFree(keyBuf);
×
535
            taosArrayDestroy(qa);
×
536
            taosWUnLockLatch(&pTaskInfo->lock);
×
537
            return code;
×
538
          }
539
        }
540
      }
541

542
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
227✔
543
      if (code != TSDB_CODE_SUCCESS) {
227!
544
        taosMemoryFree(keyBuf);
×
545
        taosArrayDestroy(qa);
×
546
        taosWUnLockLatch(&pTaskInfo->lock);
×
547
        return code;
×
548
      }
549
    }
550

551
    taosWUnLockLatch(&pTaskInfo->lock);
231✔
552
    if (keyBuf != NULL) {
231!
553
      taosMemoryFree(keyBuf);
×
554
    }
555

556
    taosArrayDestroy(qa);
231✔
557
  } else {  // remove the table id in current list
558
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
7!
559
    taosWLockLatch(&pTaskInfo->lock);
7✔
560
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
7✔
561
    taosWUnLockLatch(&pTaskInfo->lock);
7✔
562
  }
563

564
  return code;
238✔
565
}
566

567
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
2,762,640✔
568
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
569
                                    int32_t idx, bool* tbGet) {
570
  *tbGet = false;
2,762,640✔
571

572
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
2,762,640!
573
    return TSDB_CODE_INVALID_PARA;
×
574
  }
575
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,762,786✔
576

577
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
2,762,786✔
578
    return TSDB_CODE_SUCCESS;
1,597,721✔
579
  }
580

581
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
1,165,107✔
582
  if (!pSchemaInfo) {
1,165,135!
583
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
584
    return terrno;
×
585
  }
586

587
  *sversion = pSchemaInfo->sw->version;
1,165,167✔
588
  *tversion = pSchemaInfo->tversion;
1,165,167✔
589
  *rversion = pSchemaInfo->rversion;
1,165,167✔
590
  if (pSchemaInfo->dbname) {
1,165,167!
591
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
1,165,167✔
592
  } else {
593
    dbName[0] = 0;
×
594
  }
595
  if (pSchemaInfo->tablename) {
1,165,167!
596
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
1,165,178✔
597
  } else {
598
    tableName[0] = 0;
×
599
  }
600

601
  *tbGet = true;
1,165,167✔
602

603
  return TSDB_CODE_SUCCESS;
1,165,167✔
604
}
605

606
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
1,597,712✔
607

608
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
609
  if (NULL == pParam) {
×
610
    return;
×
611
  }
612
  freeOperatorParam(pParam, OP_GET_PARAM);
×
613
}
614

615
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
1,374✔
616
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
1,374✔
617
  ((SExecTaskInfo*)tinfo)->paramSet = false;
1,374✔
618
}
1,374✔
619

620
int32_t qExecutorInit(void) {
14,756✔
621
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
14,756✔
622
  return TSDB_CODE_SUCCESS;
14,759✔
623
}
624

625
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
1,603,505✔
626
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
627
                        EOPTR_EXEC_MODEL model) {
628
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
1,603,505✔
629
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
1,603,505✔
630

631
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
1,603,806✔
632

633
  readHandle->uid = 0;
1,605,375✔
634
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
1,605,375✔
635
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
1,604,871!
636
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
637
    goto _error;
37✔
638
  }
639

640
  if (handle) {
1,605,409✔
641
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
1,604,490✔
642
    void*           pSinkManager = NULL;
1,604,490✔
643
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
1,604,490✔
644
    if (code != TSDB_CODE_SUCCESS) {
1,604,548!
645
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
646
      goto _error;
×
647
    }
648

649
    void* pSinkParam = NULL;
1,604,548✔
650
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
1,604,548✔
651
    if (code != TSDB_CODE_SUCCESS) {
1,603,709!
652
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
653
      taosMemoryFree(pSinkManager);
×
654
      goto _error;
×
655
    }
656

657
    SDataSinkNode* pSink = NULL;
1,603,709✔
658
    if (readHandle->localExec) {
1,603,709!
659
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
×
660
      if (code != TSDB_CODE_SUCCESS) {
×
661
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
153!
662
               (*pTask)->id.str);
663
        taosMemoryFree(pSinkManager);
153!
664
        goto _error;
×
665
      }
666
    }
667

668
    // pSinkParam has been freed during create sinker.
669
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
1,603,556✔
670
                              (*pTask)->id.str, pSubplan->processOneBlock);
1,603,556!
671
    if (code) {
1,603,756✔
672
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
1!
673
    }
674
  }
675

676
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
1,604,317✔
677
         tstrerror(code));
678

679
_error:
118,734✔
680
  // if failed to add ref for all tables in this query, abort current query
681
  return code;
1,605,673✔
682
}
683

684
static void freeBlock(void* param) {
×
685
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
686
  blockDataDestroy(pBlock);
×
687
}
×
688

689
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
1,704,168✔
690
                     bool processOneBlock) {
691
  int32_t        code = TSDB_CODE_SUCCESS;
1,704,168✔
692
  int32_t        lino = 0;
1,704,168✔
693
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,704,168✔
694
  int64_t        threadId = taosGetSelfPthreadId();
1,704,168✔
695

696
  if (pLocal) {
1,704,194✔
697
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
1,688,968✔
698
  }
699

700
  taosArrayClear(pResList);
1,704,194✔
701

702
  int64_t curOwner = 0;
1,704,102✔
703
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
1,704,102!
704
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
705
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
706
    return pTaskInfo->code;
×
707
  }
708

709
  if (pTaskInfo->cost.start == 0) {
1,704,225✔
710
    pTaskInfo->cost.start = taosGetTimestampUs();
1,588,319✔
711
  }
712

713
  if (isTaskKilled(pTaskInfo)) {
1,704,088✔
714
    atomic_store_64(&pTaskInfo->owner, 0);
14✔
715
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
14!
716
    return pTaskInfo->code;
14✔
717
  }
718

719
  // error occurs, record the error code and return to client
720
  int32_t ret = setjmp(pTaskInfo->env);
1,704,138✔
721
  if (ret != TSDB_CODE_SUCCESS) {
1,705,185✔
722
    pTaskInfo->code = ret;
1,011✔
723
    (void)cleanUpUdfs();
1,011✔
724

725
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,011✔
726
    atomic_store_64(&pTaskInfo->owner, 0);
1,011✔
727

728
    return pTaskInfo->code;
1,011✔
729
  }
730

731
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
1,704,174✔
732

733
  int32_t      current = 0;
1,704,179✔
734
  SSDataBlock* pRes = NULL;
1,704,179✔
735
  int64_t      st = taosGetTimestampUs();
1,704,265✔
736

737
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
1,704,265!
738
    pTaskInfo->paramSet = true;
1,374✔
739
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
1,374✔
740
  } else {
741
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
1,702,891✔
742
  }
743

744
  QUERY_CHECK_CODE(code, lino, _end);
1,703,136!
745
  code = blockDataCheck(pRes);
1,703,136✔
746
  QUERY_CHECK_CODE(code, lino, _end);
1,703,182!
747

748
  if (pRes == NULL) {
1,703,182✔
749
    st = taosGetTimestampUs();
303,389✔
750
  }
751

752
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
1,703,187✔
753
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
1,703,187✔
754
    rowsThreshold = 4096;
1,693,491✔
755
  }
756

757
  int32_t blockIndex = 0;
1,703,187✔
758
  while (pRes != NULL) {
4,958,661✔
759
    SSDataBlock* p = NULL;
3,371,414✔
760
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
3,371,414✔
761
      SSDataBlock* p1 = NULL;
2,848,931✔
762
      code = createOneDataBlock(pRes, true, &p1);
2,848,931✔
763
      QUERY_CHECK_CODE(code, lino, _end);
2,848,682!
764

765
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
2,848,682✔
766
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,848,660!
767
      p = p1;
2,848,660✔
768
    } else {
769
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
522,713✔
770
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
522,500!
771

772
      p = *(SSDataBlock**)tmp;
522,500✔
773
      code = copyDataBlock(p, pRes);
522,500✔
774
      QUERY_CHECK_CODE(code, lino, _end);
524,058!
775
    }
776

777
    blockIndex += 1;
3,372,718✔
778

779
    current += p->info.rows;
3,372,718✔
780
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
3,372,718!
781
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
782
    void* tmp = taosArrayPush(pResList, &p);
3,372,553✔
783
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,372,553!
784

785
    if (current >= rowsThreshold || processOneBlock) {
3,372,553✔
786
      break;
787
    }
788

789
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
3,256,610✔
790
    QUERY_CHECK_CODE(code, lino, _end);
3,256,393!
791
    code = blockDataCheck(pRes);
3,256,393✔
792
    QUERY_CHECK_CODE(code, lino, _end);
3,255,474!
793
  }
794

795
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
1,703,190✔
796
    pTaskInfo->pSubplan->rowsThreshold -= current;
9,725✔
797
  }
798

799
  *hasMore = (pRes != NULL);
1,703,190✔
800
  uint64_t el = (taosGetTimestampUs() - st);
1,703,204✔
801

802
  pTaskInfo->cost.elapsedTime += el;
1,703,204✔
803
  if (NULL == pRes) {
1,703,204✔
804
    *useconds = pTaskInfo->cost.elapsedTime;
1,587,178✔
805
  }
806

807
_end:
116,026✔
808
  (void)cleanUpUdfs();
1,703,204✔
809

810
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
1,703,291✔
811
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
1,703,291✔
812
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
813

814
  atomic_store_64(&pTaskInfo->owner, 0);
1,703,293✔
815
  if (code) {
1,703,294!
816
    pTaskInfo->code = code;
×
817
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
818
  }
819

820
  return pTaskInfo->code;
1,703,294✔
821
}
822

823
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
824
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
825
  SArray*        pList = pTaskInfo->pResultBlockList;
×
826
  size_t         num = taosArrayGetSize(pList);
×
827
  for (int32_t i = 0; i < num; ++i) {
×
828
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
829
    if (p) {
×
830
      blockDataDestroy(*p);
×
831
    }
832
  }
833

834
  taosArrayClear(pTaskInfo->pResultBlockList);
×
835
}
×
836

837
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
207,244✔
838
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
207,244✔
839
  int64_t        threadId = taosGetSelfPthreadId();
207,244✔
840
  int64_t        curOwner = 0;
207,244✔
841

842
  *pRes = NULL;
207,244✔
843

844
  // todo extract method
845
  taosRLockLatch(&pTaskInfo->lock);
207,244✔
846
  bool isKilled = isTaskKilled(pTaskInfo);
207,244✔
847
  if (isKilled) {
207,242!
848
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
849

850
    taosRUnLockLatch(&pTaskInfo->lock);
×
851
    return pTaskInfo->code;
×
852
  }
853

854
  if (pTaskInfo->owner != 0) {
207,242!
855
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
856
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
857

858
    taosRUnLockLatch(&pTaskInfo->lock);
×
859
    return pTaskInfo->code;
×
860
  }
861

862
  pTaskInfo->owner = threadId;
207,242✔
863
  taosRUnLockLatch(&pTaskInfo->lock);
207,242✔
864

865
  if (pTaskInfo->cost.start == 0) {
207,246✔
866
    pTaskInfo->cost.start = taosGetTimestampUs();
714✔
867
  }
868

869
  // error occurs, record the error code and return to client
870
  int32_t ret = setjmp(pTaskInfo->env);
207,246✔
871
  if (ret != TSDB_CODE_SUCCESS) {
207,242!
872
    pTaskInfo->code = ret;
×
873
    (void)cleanUpUdfs();
×
874
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
875
    atomic_store_64(&pTaskInfo->owner, 0);
×
876
    return pTaskInfo->code;
×
877
  }
878

879
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
207,242✔
880

881
  int64_t st = taosGetTimestampUs();
207,245✔
882
  int32_t code = TSDB_CODE_SUCCESS;
207,245✔
883
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
207,245!
884
    pTaskInfo->paramSet = true;
×
885
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
886
  } else {
887
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
207,245✔
888
  }
889
  if (code) {
207,226!
890
    pTaskInfo->code = code;
×
891
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
892
  }
893

894
  code = blockDataCheck(*pRes);
207,226✔
895
  if (code) {
207,213!
896
    pTaskInfo->code = code;
×
897
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
898
  }
899

900
  uint64_t el = (taosGetTimestampUs() - st);
207,223✔
901

902
  pTaskInfo->cost.elapsedTime += el;
207,223✔
903
  if (NULL == *pRes) {
207,223✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
19,453✔
905
  }
906

907
  (void)cleanUpUdfs();
207,223✔
908

909
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
207,245✔
910
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
207,245✔
911

912
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
207,245!
913
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
914

915
  atomic_store_64(&pTaskInfo->owner, 0);
207,243✔
916
  return pTaskInfo->code;
207,241✔
917
}
918

919
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
579,338✔
920
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
579,338✔
921
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
579,351✔
922
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
579,347✔
923

924
  if (!tmp) {
579,352✔
925
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2!
926
    return terrno;
2✔
927
  }
928
  return TSDB_CODE_SUCCESS;
579,350✔
929
}
930

931
int32_t stopInfoComp(void const* lp, void const* rp) {
×
932
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
933
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
934

935
  if (key->refId < pInfo->refId) {
×
936
    return -1;
×
937
  } else if (key->refId > pInfo->refId) {
×
938
    return 1;
×
939
  }
940

941
  return 0;
×
942
}
943

944
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
945
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
946
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
947
  if (idx >= 0) {
×
948
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
949
  }
950
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
951
}
×
952

953
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
1,005✔
954
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
1,005✔
955

956
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
1,005✔
957
  for (int32_t i = 0; i < num; ++i) {
1,034✔
958
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
29✔
959
    if (!pStop) {
29!
960
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
961
      continue;
×
962
    }
963
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
29✔
964
    if (pExchangeInfo) {
29!
965
      int32_t code = tsem_post(&pExchangeInfo->ready);
29✔
966
      if (code != TSDB_CODE_SUCCESS) {
29!
967
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
968
      } else {
969
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
29✔
970
      }
971
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
29✔
972
      if (code != TSDB_CODE_SUCCESS) {
29!
973
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
974
      }
975
    }
976
  }
977

978
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
1,005✔
979
}
1,005✔
980

981
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
1,005✔
982
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
1,005✔
983
  if (pTaskInfo == NULL) {
1,005!
984
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
985
  }
986

987
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
1,005✔
988

989
  setTaskKilled(pTaskInfo, rspCode);
1,005✔
990
  qStopTaskOperators(pTaskInfo);
1,005✔
991

992
  return TSDB_CODE_SUCCESS;
1,005✔
993
}
994

995
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
996
  int64_t        st = taosGetTimestampMs();
×
997
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
998
  if (pTaskInfo == NULL) {
×
999
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1000
  }
1001

1002
  if (waitDuration > 0) {
×
1003
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1004
  } else {
1005
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1006
  }
1007

1008
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1009

1010
  if (waitDuration > 0) {
×
1011
    while (1) {
1012
      taosWLockLatch(&pTaskInfo->lock);
×
1013
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1014
        taosWUnLockLatch(&pTaskInfo->lock);
×
1015

1016
        taosMsleep(200);
×
1017

1018
        int64_t d = taosGetTimestampMs() - st;
×
1019
        if (d >= waitDuration && waitDuration >= 0) {
×
1020
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1021
          return TSDB_CODE_SUCCESS;
×
1022
        }
1023
      } else {  // not running now
1024
        pTaskInfo->code = rspCode;
×
1025
        taosWUnLockLatch(&pTaskInfo->lock);
×
1026
        return TSDB_CODE_SUCCESS;
×
1027
      }
1028
    }
1029
  }
1030

1031
  int64_t et = taosGetTimestampMs() - st;
×
1032
  if (et < waitDuration) {
×
1033
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1034
    return TSDB_CODE_SUCCESS;
×
1035
  }
1036
  return TSDB_CODE_SUCCESS;
×
1037
}
1038

1039
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1040
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1041
  if (NULL == pTaskInfo) {
×
1042
    return false;
×
1043
  }
1044

1045
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1046
}
1047

1048
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
1,622,219✔
1049
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1,622,219✔
1050
  int64_t        idleTime = pSummary->start - pSummary->created;
1,622,219✔
1051

1052
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
1,622,219✔
1053
  if (pSummary->pRecoder != NULL) {
1,622,219✔
1054
    qDebug(
1,149,795✔
1055
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1056
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1057
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1058
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1059
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1060
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1061
  } else {
1062
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
472,424✔
1063
           pSummary->elapsedTime / 1000.0);
1064
  }
1065
}
1,622,219✔
1066

1067
void qDestroyTask(qTaskInfo_t qTaskHandle) {
1,626,521✔
1068
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
1,626,521✔
1069
  if (pTaskInfo == NULL) {
1,626,521✔
1070
    return;
4,301✔
1071
  }
1072

1073
  if (pTaskInfo->pRoot != NULL) {
1,622,220!
1074
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
1,622,229✔
1075
  } else {
1076
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1077
  }
1078

1079
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
1,622,220✔
1080
  doDestroyTask(pTaskInfo);
1,622,221✔
1081
}
1082

1083
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
27,228✔
1084
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
27,228✔
1085
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
27,228✔
1086
}
1087

1088
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
1,017✔
1089
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,017✔
1090
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
1,017✔
1091

1092
  while (1) {
994✔
1093
    uint16_t type = pOperator->operatorType;
2,011✔
1094
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
2,011✔
1095
      *scanner = pOperator->info;
1,017✔
1096
      break;
1,017✔
1097
    } else {
1098
      pOperator = pOperator->pDownstream[0];
994✔
1099
    }
1100
  }
1101
}
1,017✔
1102

1103
static int32_t getOpratorIntervalInfo(SOperatorInfo* pOperator, int64_t* pWaterMark, SInterval* pInterval,
×
1104
                                      STimeWindow* pLastWindow, TSKEY* pRecInteral) {
1105
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1106
    return getOpratorIntervalInfo(pOperator->pDownstream[0], pWaterMark, pInterval, pLastWindow, pRecInteral);
×
1107
  }
1108
  SStreamScanInfo* pScanOp = (SStreamScanInfo*)pOperator->info;
×
1109
  *pWaterMark = pScanOp->twAggSup.waterMark;
×
1110
  *pInterval = pScanOp->interval;
×
1111
  *pLastWindow = pScanOp->lastScanRange;
×
1112
  *pRecInteral = pScanOp->recalculateInterval;
×
1113
  return TSDB_CODE_SUCCESS;
×
1114
}
1115

1116
void* qExtractReaderFromTmqScanner(void* scanner) {
1,017✔
1117
  SStreamScanInfo* pInfo = scanner;
1,017✔
1118
  return (void*)pInfo->tqReader;
1,017✔
1119
}
1120

1121
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
2,531✔
1122
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,531✔
1123
  return pTaskInfo->streamInfo.schema;
2,531✔
1124
}
1125

1126
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
2,531✔
1127
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,531✔
1128
  return pTaskInfo->streamInfo.tbName;
2,531✔
1129
}
1130

1131
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
2,635✔
1132
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,635✔
1133
  return &pTaskInfo->streamInfo.btMetaRsp;
2,635✔
1134
}
1135

1136
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
21,920✔
1137
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,920✔
1138
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
21,920✔
1139
  return 0;
21,920✔
1140
  /*if (code != TSDB_CODE_SUCCESS) {
1141
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1142
    pTaskInfo->code = code;
1143
    T_LONG_JMP(pTaskInfo->env, code);
1144
  }*/
1145
}
1146

1147
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
2,510✔
1148
  memset(pCond, 0, sizeof(SQueryTableDataCond));
2,510✔
1149
  pCond->order = TSDB_ORDER_ASC;
2,510✔
1150
  pCond->numOfCols = pMtInfo->schema->nCols;
2,510✔
1151
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
2,510!
1152
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
2,513!
1153
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
2,512!
1154
    taosMemoryFreeClear(pCond->colList);
×
1155
    taosMemoryFreeClear(pCond->pSlotList);
×
1156
    return terrno;
×
1157
  }
1158

1159
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
2,512✔
1160
  pCond->suid = pMtInfo->suid;
2,512✔
1161
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
2,512✔
1162
  pCond->startVersion = -1;
2,512✔
1163
  pCond->endVersion = sContext->snapVersion;
2,512✔
1164

1165
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
15,822✔
1166
    SColumnInfo* pColInfo = &pCond->colList[i];
13,310✔
1167
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
13,310✔
1168
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
13,310✔
1169
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
13,310✔
1170
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
13,310✔
1171

1172
    pCond->pSlotList[i] = i;
13,310✔
1173
  }
1174

1175
  return TSDB_CODE_SUCCESS;
2,512✔
1176
}
1177

1178
void qStreamSetOpen(qTaskInfo_t tinfo) {
202,173✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
202,173✔
1180
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
202,173✔
1181
  pOperator->status = OP_NOT_OPENED;
202,173✔
1182
}
202,173✔
1183

1184
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
19,227✔
1185
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,227✔
1186
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
19,227✔
1187
}
19,227✔
1188

1189
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
22,611✔
1190
  int32_t        code = TSDB_CODE_SUCCESS;
22,611✔
1191
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,611✔
1192
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
22,611✔
1193

1194
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,611✔
1195
  const char*    id = GET_TASKID(pTaskInfo);
22,611✔
1196

1197
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,611✔
1198
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
18,364✔
1199
    if (pOperator == NULL || code != 0) {
18,364!
1200
      return code;
×
1201
    }
1202

1203
    SStreamScanInfo* pInfo = pOperator->info;
18,364✔
1204
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
18,364✔
1205
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
18,364✔
1206
    walReaderVerifyOffset(pWalReader, pOffset);
18,364✔
1207
  }
1208
  // if pOffset equal to current offset, means continue consume
1209
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,611✔
1210
    return 0;
17,915✔
1211
  }
1212

1213
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
4,696✔
1214
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
2,135✔
1215
    if (pOperator == NULL || code != 0) {
2,135!
1216
      return code;
×
1217
    }
1218

1219
    SStreamScanInfo* pInfo = pOperator->info;
2,135✔
1220
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
2,135✔
1221
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
2,135✔
1222
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
2,135✔
1223

1224
    if (pOffset->type == TMQ_OFFSET__LOG) {
2,135✔
1225
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,315✔
1226
      pScanBaseInfo->dataReader = NULL;
1,315✔
1227

1228
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
1,315✔
1229
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
1,315✔
1230
      walReaderVerifyOffset(pWalReader, pOffset);
1,315✔
1231
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
1,315✔
1232
      if (code < 0) {
1,315✔
1233
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
29!
1234
        return code;
29✔
1235
      }
1236
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
820!
1237
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1238
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1239
      int64_t uid = pOffset->uid;
820✔
1240
      int64_t ts = pOffset->ts;
820✔
1241
      int32_t index = 0;
820✔
1242

1243
      // this value may be changed if new tables are created
1244
      taosRLockLatch(&pTaskInfo->lock);
820✔
1245
      int32_t numOfTables = 0;
820✔
1246
      code = tableListGetSize(pTableListInfo, &numOfTables);
820✔
1247
      if (code != TSDB_CODE_SUCCESS) {
820!
1248
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1249
        taosRUnLockLatch(&pTaskInfo->lock);
×
1250
        return code;
676✔
1251
      }
1252

1253
      if (uid == 0) {
820✔
1254
        if (numOfTables != 0) {
798✔
1255
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
122✔
1256
          if (!tmp) {
122!
1257
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1258
            taosRUnLockLatch(&pTaskInfo->lock);
×
1259
            return terrno;
×
1260
          }
1261
          if (tmp) uid = tmp->uid;
122!
1262
          ts = INT64_MIN;
122✔
1263
          pScanInfo->currentTable = 0;
122✔
1264
        } else {
1265
          taosRUnLockLatch(&pTaskInfo->lock);
676✔
1266
          qError("no table in table list, %s", id);
676!
1267
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
676✔
1268
        }
1269
      }
1270
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
144✔
1271

1272
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
144!
1273
             pInfo->pTableScanOp->resultInfo.totalRows);
1274
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
144✔
1275

1276
      // start from current accessed position
1277
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1278
      // position, let's find it from the beginning.
1279
      index = tableListFind(pTableListInfo, uid, 0);
144✔
1280
      taosRUnLockLatch(&pTaskInfo->lock);
144✔
1281

1282
      if (index >= 0) {
144!
1283
        pScanInfo->currentTable = index;
144✔
1284
      } else {
1285
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1286
               numOfTables, pScanInfo->currentTable, id);
1287
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1288
      }
1289

1290
      STableKeyInfo keyInfo = {.uid = uid};
144✔
1291
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
144✔
1292

1293
      // let's start from the next ts that returned to consumer.
1294
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
144!
1295
        pScanBaseInfo->cond.twindows.skey = ts;
×
1296
      } else {
1297
        pScanBaseInfo->cond.twindows.skey = ts + 1;
144✔
1298
      }
1299
      pScanInfo->scanTimes = 0;
144✔
1300

1301
      if (pScanBaseInfo->dataReader == NULL) {
144✔
1302
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
123✔
1303
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1304
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
123✔
1305
        if (code != TSDB_CODE_SUCCESS) {
123!
1306
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1307
          return code;
×
1308
        }
1309

1310
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
123!
1311
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1312
      } else {
1313
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
21✔
1314
        if (code != TSDB_CODE_SUCCESS) {
21!
1315
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1316
          return code;
×
1317
        }
1318

1319
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
21✔
1320
        if (code != TSDB_CODE_SUCCESS) {
21!
1321
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1322
          return code;
×
1323
        }
1324
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
21!
1325
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1326
      }
1327

1328
      // restore the key value
1329
      pScanBaseInfo->cond.twindows.skey = oldSkey;
144✔
1330
    } else {
1331
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1332
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1333
    }
1334

1335
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1336
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
2,561✔
1337
      SStreamRawScanInfo* pInfo = pOperator->info;
2,516✔
1338
      SSnapContext*       sContext = pInfo->sContext;
2,516✔
1339
      SOperatorInfo*      p = NULL;
2,516✔
1340

1341
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
2,516✔
1342
      if (code != 0) {
2,516!
1343
        return code;
×
1344
      }
1345

1346
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
2,516✔
1347

1348
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
2,516!
1349
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1350
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1351
      }
1352

1353
      SMetaTableInfo mtInfo = {0};
2,515✔
1354
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
2,515✔
1355
      if (code != 0) {
2,515!
1356
        destroyMetaTableInfo(&mtInfo);
1357
        return code;
×
1358
      }
1359
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,515✔
1360
      pInfo->dataReader = NULL;
2,516✔
1361

1362
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,516✔
1363
      tableListClear(pTableListInfo);
2,515✔
1364

1365
      if (mtInfo.uid == 0) {
2,516✔
1366
        destroyMetaTableInfo(&mtInfo);
1367
        goto end;  // no data
3✔
1368
      }
1369

1370
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
2,513✔
1371
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
2,511✔
1372
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1373
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1374
        destroyMetaTableInfo(&mtInfo);
1375
        return code;
×
1376
      }
1377
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
2,513!
1378
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1379
      } else {
1380
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
2,511✔
1381
      }
1382

1383
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
2,511✔
1384
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1385
        destroyMetaTableInfo(&mtInfo);
1386
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1387
        return code;
×
1388
      }
1389

1390
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
2,513✔
1391
      if (!pList) {
2,513!
1392
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1393
        destroyMetaTableInfo(&mtInfo);
1394
        return code;
×
1395
      }
1396
      int32_t size = 0;
2,513✔
1397
      code = tableListGetSize(pTableListInfo, &size);
2,513✔
1398
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1399
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1400
        destroyMetaTableInfo(&mtInfo);
1401
        return code;
×
1402
      }
1403

1404
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
2,513✔
1405
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
2,513✔
1406
      if (code != TSDB_CODE_SUCCESS) {
2,511!
1407
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1408
        destroyMetaTableInfo(&mtInfo);
1409
        return code;
×
1410
      }
1411

1412
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,511✔
1413
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
2,511✔
1414
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1415
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
2,511✔
1416
      pTaskInfo->streamInfo.schema = mtInfo.schema;
2,513✔
1417

1418
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
2,513✔
1419
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
45✔
1420
      SStreamRawScanInfo* pInfo = pOperator->info;
16✔
1421
      SSnapContext*       sContext = pInfo->sContext;
16✔
1422
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
16✔
1423
      if (code != 0) {
16!
1424
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1425
        return code;
×
1426
      }
1427
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
16!
1428
             id);
1429
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
29!
1430
      SStreamRawScanInfo* pInfo = pOperator->info;
29✔
1431
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
29✔
1432
      pInfo->dataReader = NULL;
29✔
1433
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
29!
1434
    }
1435
  }
1436

1437
end:
×
1438
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
3,991✔
1439
  return 0;
3,991✔
1440
}
1441

1442
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
940,091✔
1443
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
940,091✔
1444
  if (pMsg->info.ahandle == NULL) {
940,091!
1445
    qError("pMsg->info.ahandle is NULL");
×
1446
    return;
×
1447
  }
1448

1449
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
940,091✔
1450
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1451

1452
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
940,095✔
1453

1454
  if (pMsg->contLen > 0) {
940,095!
1455
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
940,132!
1456
    if (buf.pData == NULL) {
940,087!
1457
      pMsg->code = terrno;
×
1458
    } else {
1459
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
940,087✔
1460
    }
1461
  }
1462

1463
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
940,050✔
1464
  rpcFreeCont(pMsg->pCont);
940,203✔
1465
  destroySendMsgInfo(pSendInfo);
940,157✔
1466
}
1467

1468
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1469
  int32_t        code = TSDB_CODE_SUCCESS;
×
1470
  int32_t        lino = 0;
×
1471
  SExecTaskInfo* pTaskInfo = tinfo;
×
1472
  SArray*        plist = NULL;
×
1473

1474
  code = getTableListInfo(pTaskInfo, &plist);
×
1475
  if (code || plist == NULL) {
×
1476
    return NULL;
×
1477
  }
1478

1479
  // only extract table in the first elements
1480
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1481

1482
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1483
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1484

1485
  int32_t numOfTables = 0;
×
1486
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1487
  QUERY_CHECK_CODE(code, lino, _end);
×
1488

1489
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1490
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1491
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1492
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1493
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1494
  }
1495

1496
  taosArrayDestroy(plist);
×
1497

1498
_end:
×
1499
  if (code != TSDB_CODE_SUCCESS) {
×
1500
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1501
    T_LONG_JMP(pTaskInfo->env, code);
×
1502
  }
1503
  return pUidList;
×
1504
}
1505

1506
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
14,025✔
1507
  int32_t        code = TSDB_CODE_SUCCESS;
14,025✔
1508
  int32_t        lino = 0;
14,025✔
1509
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,025✔
1510

1511
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,025!
1512
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1513
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1514

1515
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1516
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1517
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
14,025✔
1518
    STableScanInfo* pScanInfo = pOperator->info;
7,013✔
1519

1520
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
7,013✔
1521
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,013!
1522
  } else {
1523
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
7,012!
1524
      code = extractTableList(pList, pOperator->pDownstream[0]);
7,013✔
1525
    }
1526
  }
1527

1528
_end:
×
1529
  if (code != TSDB_CODE_SUCCESS) {
14,024!
1530
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1531
  }
1532
  return code;
14,024✔
1533
}
1534

1535
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
7,012✔
1536
  if (pList == NULL) {
7,012!
1537
    return TSDB_CODE_INVALID_PARA;
×
1538
  }
1539

1540
  *pList = NULL;
7,012✔
1541
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
7,012✔
1542
  if (pArray == NULL) {
7,013!
1543
    return terrno;
×
1544
  }
1545

1546
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
7,013✔
1547
  if (code == 0) {
7,012!
1548
    *pList = pArray;
7,012✔
1549
  } else {
1550
    taosArrayDestroy(pArray);
×
1551
  }
1552
  return code;
7,012✔
1553
}
1554

1555
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1556
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1557
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1558
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1559
  }
1560
  return 0;
×
1561
}
1562

1563
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1564
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1565
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1566
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1567
  }
1568
  return 0;
×
1569
}
1570

1571
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1572
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1573

1574
  int32_t code = pTaskInfo->code;
×
1575
  pTaskInfo->code = 0;
×
1576
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1577
}
×
1578

1579
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1580
  int32_t code = 0;
×
1581
  return code;
×
1582
}
1583

1584
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1585
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1586
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1587
  return code;
×
1588
}
1589

1590
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
57,694✔
1591
  int32_t code = 0;
57,694✔
1592

1593
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
57,694✔
1594
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
57,694✔
1595

1596
  if (pOper->fpSet.resetStateFn) {
57,694!
1597
    code = pOper->fpSet.resetStateFn(pOper);
57,694✔
1598
  }
1599
  pOper->status = OP_NOT_OPENED;
57,693✔
1600
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
91,421!
1601
    code = clearStatesForOperator(pOper->pDownstream[i]);
33,728✔
1602
  }
1603
  return code;
57,693✔
1604
}
1605

1606
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
23,966✔
1607
  int32_t        code = 0;
23,966✔
1608
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
23,966✔
1609
  SOperatorInfo* pOper = pTaskInfo->pRoot;
23,966✔
1610
  code = clearStatesForOperator(pOper);
23,966✔
1611
  return code;
23,966✔
1612
}
1613

1614
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
36,464✔
1615
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
36,464✔
1616
  int64_t        threadId = taosGetSelfPthreadId();
36,464✔
1617
  int64_t        curOwner = 0;
36,464✔
1618

1619
  *ppRes = NULL;
36,464✔
1620

1621
  // todo extract method
1622
  taosRLockLatch(&pTaskInfo->lock);
36,464✔
1623
  bool isKilled = isTaskKilled(pTaskInfo);
36,464✔
1624
  if (isKilled) {
36,464!
1625
    // clearStreamBlock(pTaskInfo->pRoot);
1626
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1627

1628
    taosRUnLockLatch(&pTaskInfo->lock);
×
1629
    return pTaskInfo->code;
×
1630
  }
1631

1632
  if (pTaskInfo->owner != 0) {
36,464!
1633
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1634
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1635

1636
    taosRUnLockLatch(&pTaskInfo->lock);
×
1637
    return pTaskInfo->code;
×
1638
  }
1639

1640
  pTaskInfo->owner = threadId;
36,464✔
1641
  taosRUnLockLatch(&pTaskInfo->lock);
36,464✔
1642

1643
  if (pTaskInfo->cost.start == 0) {
36,464✔
1644
    pTaskInfo->cost.start = taosGetTimestampUs();
922✔
1645
  }
1646

1647
  // error occurs, record the error code and return to client
1648
  int32_t ret = setjmp(pTaskInfo->env);
36,464✔
1649
  if (ret != TSDB_CODE_SUCCESS) {
36,474✔
1650
    pTaskInfo->code = ret;
10✔
1651
    (void)cleanUpUdfs();
10✔
1652
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
10✔
1653
    atomic_store_64(&pTaskInfo->owner, 0);
10✔
1654
    return pTaskInfo->code;
10✔
1655
  }
1656

1657
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
36,464✔
1658

1659
  int64_t st = taosGetTimestampUs();
36,464✔
1660

1661
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
36,464✔
1662
  if (code) {
36,453!
1663
    pTaskInfo->code = code;
×
1664
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1665
  } else {
1666
    *finished = *ppRes == NULL;
36,453✔
1667
    code = blockDataCheck(*ppRes);
36,453✔
1668
  }
1669
  if (code) {
36,454!
1670
    pTaskInfo->code = code;
×
1671
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1672
  }
1673

1674
  uint64_t el = (taosGetTimestampUs() - st);
36,454✔
1675

1676
  pTaskInfo->cost.elapsedTime += el;
36,454✔
1677
  if (NULL == *ppRes) {
36,454✔
1678
    *useconds = pTaskInfo->cost.elapsedTime;
23,420✔
1679
  }
1680

1681
  (void)cleanUpUdfs();
36,454✔
1682

1683
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
36,454✔
1684
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
36,454✔
1685

1686
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
36,454✔
1687
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1688

1689
  atomic_store_64(&pTaskInfo->owner, 0);
36,454✔
1690
  return pTaskInfo->code;
36,454✔
1691
}
1692

1693
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1694
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1695
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1696
// }
1697

1698
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
37,602✔
1699
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1700
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1701
  STableListInfo* pList = tableListCreate();
37,602✔
1702
  if (pList == NULL) {
37,551!
1703
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1704
    return terrno;
×
1705
  }
1706

1707
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
37,551✔
1708
  SReadHandle    pHandle = {.vnode = pVnode};
37,551✔
1709
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
37,551✔
1710

1711
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
37,551✔
1712
  if (code != 0) {
37,599!
1713
    tableListDestroy(pList);
×
1714
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1715
    return code;
×
1716
  }
1717
  *pTableListInfo = pList;
37,602✔
1718
  return 0;
37,602✔
1719
}
1720

1721
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
30,639✔
1722
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
30,639!
1723
    return TSDB_CODE_INVALID_PARA;
×
1724
  }
1725
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
30,640✔
1726
    *size = 0;
22,789✔
1727
    *pKeyInfo = NULL;
22,789✔
1728
    return 0;
22,789✔
1729
  }
1730
  if (currentGroupId == -1) {
7,851✔
1731
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
614✔
1732
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
614✔
1733
    return 0;
614✔
1734
  }
1735
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
7,237✔
1736
}
1737

1738
int32_t  qStreamSetTableList(void** pTableListInfo, STableKeyInfo* data){
1,609✔
1739
  if (*pTableListInfo == NULL) {
1,609✔
1740
    *pTableListInfo = tableListCreate();
489✔
1741
    if (*pTableListInfo == NULL) {
489!
1742
      return terrno;
×
1743
    }
1744
  }
1745
  return taosArrayPush(((STableListInfo*)(*pTableListInfo))->pTableList, data) != NULL ? 0 : terrno;
3,218!
1746
}
1747

1748
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
28,130✔
1749
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
28,130✔
1750
    return 0;
22,645✔
1751
  }
1752
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
7,776✔
1753
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
7,444✔
1754

1755
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
7,444✔
1756
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
7,444!
1757
      return i;
5,153✔
1758
    }
1759
  }
1760
  return -1;
332✔
1761
}
1762

1763
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
41,870✔
1764

1765
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
7,558✔
1766

1767
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
1,942✔
1768
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
548✔
1769

1770
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo) { return doFilter(pBlock, pFilterInfo, NULL); }
9,063✔
1771

1772
bool qStreamUidInTableList(void* pTableListInfo, uint64_t uid) {
36,112✔
1773
  return tableListGetTableGroupId(pTableListInfo, uid) != -1;
36,112✔
1774
}
1775

1776
void streamDestroyExecTask(qTaskInfo_t tInfo) {
3,333✔
1777
  qInfo("streamDestroyExecTask called, task:%p", tInfo);
3,333!
1778
  qDestroyTask(tInfo);
3,333✔
1779
}
3,333✔
1780

1781
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
1,807✔
1782
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
1,807✔
1783
}
1784

1785
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
1,839✔
1786
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1787
  int32_t      code = 0;
1,839✔
1788
  SNode*       pNode = 0;
1,839✔
1789
  SNodeList*   pList = NULL;
1,839✔
1790
  SExprInfo*   pExprInfo = NULL;
1,839✔
1791
  int32_t      numOfExprs = 1;
1,839✔
1792
  int32_t*     offset = 0;
1,839✔
1793
  STargetNode* pTargetNode = NULL;
1,839✔
1794
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
1,839✔
1795
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
1,839!
1796

1797
  if (code == 0) {
1,839!
1798
    pTargetNode->dataBlockId = 0;
1,839✔
1799
    pTargetNode->pExpr = pNode;
1,839✔
1800
    pTargetNode->slotId = 0;
1,839✔
1801
  }
1802
  if (code == 0) {
1,839!
1803
    code = nodesMakeList(&pList);
1,839✔
1804
  }
1805
  if (code == 0) {
1,839!
1806
    code = nodesListAppend(pList, (SNode*)pTargetNode);
1,839✔
1807
  }
1808
  if (code == 0) {
1,839!
1809
    pNode = NULL;
1,839✔
1810
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
1,839✔
1811
  }
1812

1813
  if (code == 0) {
1,839!
1814
    const char* pVal = NULL;
1,839✔
1815
    int32_t     len = 0;
1,839✔
1816
    SNode*      pSclNode = NULL;
1,839✔
1817
    switch (pExprInfo->pExpr->nodeType) {
1,839!
1818
      case QUERY_NODE_FUNCTION:
1,839✔
1819
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
1,839✔
1820
        break;
1,839✔
1821
      case QUERY_NODE_OPERATOR:
×
1822
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1823
        break;
×
1824
      default:
×
1825
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1826
        break;
×
1827
    }
1828
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
1,839✔
1829
    SSDataBlock block = {0};
1,839✔
1830
    block.info.rows = 1;
1,839✔
1831
    SSDataBlock* pBlock = &block;
1,839✔
1832
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
1,839✔
1833
    if (tmp == NULL) {
1,839!
1834
      code = terrno;
×
1835
    }
1836
    if (code == 0) {
1,839!
1837
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
1,839✔
1838
    }
1839
    taosArrayDestroy(pBlockList);
1,839✔
1840
  }
1841
  nodesDestroyList(pList);
1,839✔
1842
  destroyExprInfo(pExprInfo, numOfExprs);
1,839✔
1843
  taosMemoryFreeClear(pExprInfo);
1,839!
1844
  return code;
1,839✔
1845
}
1846

1847
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
11,072✔
1848
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,072✔
1849
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
11,072✔
1850
  int32_t        code = 0;
11,072✔
1851
  SNode*         pNode = NULL;
11,072✔
1852
  if (!pForceOutputCols) return 0;
11,072✔
1853
  if (!*pRes) {
32!
1854
    code = createDataBlock(pRes);
32✔
1855
  }
1856

1857
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
32!
1858
    int32_t idx = 0;
32✔
1859
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
127✔
1860
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
95✔
1861
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
95✔
1862
      colInfo.info.precision = pCol->type.precision;
95✔
1863
      colInfo.info.scale = pCol->type.scale;
95✔
1864
      code = blockDataAppendColInfo(*pRes, &colInfo);
95✔
1865
      if (code != 0) break;
95!
1866
    }
1867
  }
1868

1869
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
32✔
1870
  if (code != TSDB_CODE_SUCCESS) {
32!
1871
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1872
    return code;
×
1873
  }
1874

1875
  // loop all exprs for force output, execute all exprs
1876
  int32_t idx = 0;
32✔
1877
  int32_t rowIdx = (*pRes)->info.rows;
32✔
1878
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
32✔
1879
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
32✔
1880
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
127✔
1881
    SScalarParam   dst = {0};
95✔
1882
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
95✔
1883
    code = nodesStringToNode(pCol->expr, &pNode);
95✔
1884
    if (code != 0) break;
95!
1885
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
95✔
1886
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
95✔
1887
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
63✔
1888
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
63✔
1889
    } else {
1890
      dst.columnData = pInfo;
32✔
1891
      dst.numOfRows = rowIdx;
32✔
1892
      dst.colAlloced = false;
32✔
1893
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx,  rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
32✔
1894
    }
1895
    ++idx;
95✔
1896
    // TODO sclFreeParam(&dst);
1897
    nodesDestroyNode(pNode);
95✔
1898
    if (code != 0) break;
95!
1899
  }
1900
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
32✔
1901
  (*pRes)->info.rows++;
32✔
1902
  return code;
32✔
1903
}
1904

1905
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
629✔
1906
  int32_t      code = 0;
629✔
1907
  const char*  pVal = NULL;
629✔
1908
  SScalarParam dst = {0};
629✔
1909
  int32_t      len = 0;
629✔
1910
  // execute the expr
1911
  switch (pExpr->type) {
629!
1912
    case QUERY_NODE_VALUE: {
×
1913
      SValueNode* pValue = (SValueNode*)pExpr;
×
1914
      int32_t     type = pValue->node.resType.type;
×
1915
      if (!IS_STR_DATA_TYPE(type)) {
×
1916
        qError("invalid sub tb expr with non-str type");
×
1917
        code = TSDB_CODE_INVALID_PARA;
×
1918
        break;
×
1919
      }
1920
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
1921
      if (pTmp == NULL) {
×
1922
        qError("invalid sub tb expr with null value");
×
1923
        code = TSDB_CODE_INVALID_PARA;
×
1924
        break;
×
1925
      }
1926
      pVal = varDataVal(pTmp);
×
1927
      len = varDataLen(pTmp);
×
1928
    } break;
×
1929
    case QUERY_NODE_FUNCTION: {
629✔
1930
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
629✔
1931
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
629!
1932
        qError("invalid sub tb expr with non-str type func");
×
1933
        code = TSDB_CODE_INVALID_PARA;
×
1934
        break;
×
1935
      }
1936
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
629!
1937
      if (!pCol) {
629!
1938
        code = terrno;
×
1939
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
1940
        break;
×
1941
      }
1942

1943
      pCol->hasNull = true;
629✔
1944
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
629✔
1945
      pCol->info.colId = 0;
629✔
1946
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
629✔
1947
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
629✔
1948
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
629✔
1949
      code = colInfoDataEnsureCapacity(pCol, 1, true);
629✔
1950
      if (code != 0) {
629!
1951
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
1952
        taosMemoryFree(pCol);
×
1953
        break;
×
1954
      }
1955
      dst.columnData = pCol;
629✔
1956
      dst.numOfRows = 1;
629✔
1957
      dst.colAlloced = true;
629✔
1958
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
629✔
1959
      if (colDataIsNull_var(dst.columnData, 0)) {
629!
1960
        qInfo("invalid sub tb expr with null value");
×
1961
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
1962
      }
1963
      if (code == 0) {
629!
1964
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
629✔
1965
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
629✔
1966
      }
1967
    } break;
629✔
1968
    default:
×
1969
      qError("wrong subtable expr with type: %d", pExpr->type);
×
1970
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1971
      break;
×
1972
  }
1973
  if (code == 0) {
629!
1974
    if (!pVal || len == 0) {
629!
1975
      qError("tbname generated with no characters which is not allowed");
×
1976
      code = TSDB_CODE_INVALID_PARA;
×
1977
    }
1978
    if(len > TSDB_TABLE_NAME_LEN - 1) {
629✔
1979
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
2!
1980
      len = TSDB_TABLE_NAME_LEN - 1;
2✔
1981
    }
1982

1983
    memcpy(tbname, pVal, len);
629✔
1984
    tbname[len] = '\0';  // ensure null terminated
629✔
1985
    if (NULL != strchr(tbname, '.')) {
629!
1986
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
1987
      qError("tbname generated with invalid characters, '.' is not allowed");
×
1988
    }
1989
  }
1990
  // TODO free dst
1991
  sclFreeParam(&dst);
629✔
1992
  return code;
629✔
1993
}
1994

1995
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
914✔
1996
  if (pParam) {
914!
1997
    if (pParam->tbname) {
914!
1998
      taosMemFree(pParam->tbname);
914✔
1999
      pParam->tbname = NULL;
914✔
2000
    }
2001
    if (pParam->stbname) {
914!
2002
      taosMemFree(pParam->stbname);
914✔
2003
      pParam->stbname = NULL;
914✔
2004
    }
2005
    if (pParam->dbFName) {
914!
2006
      taosMemFree(pParam->dbFName);
914✔
2007
      pParam->dbFName = NULL;
914✔
2008
    }
2009
    if (pParam->pFields) {
914!
2010
      taosArrayDestroy(pParam->pFields);
914✔
2011
      pParam->pFields = NULL;
914✔
2012
    }
2013
    if (pParam->pTagFields) {
914✔
2014
      taosArrayDestroy(pParam->pTagFields);
574✔
2015
      pParam->pTagFields = NULL;
574✔
2016
    }
2017
    taosMemFree(pParam);
914✔
2018
  }
2019
}
914✔
2020

2021
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
914✔
2022
  int32_t code = 0, lino = 0;
914✔
2023
  if (ppDst == NULL || pSrc == NULL) {
914!
2024
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2025
  }
2026
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
914!
2027
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
914!
2028

2029
  (*ppDst)->suid = pSrc->suid;
914✔
2030
  (*ppDst)->sver = pSrc->sver;
914✔
2031
  (*ppDst)->tbType = pSrc->tbType;
914✔
2032
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
914!
2033
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
914!
2034

2035
  if (pSrc->stbname) {
914!
2036
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
914!
2037
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
914!
2038
  }
2039

2040
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
914!
2041
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
914!
2042

2043
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
914✔
2044

2045
  if (pSrc->pFields && pSrc->pFields->size > 0) {
914!
2046
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
914✔
2047
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
914!
2048
  } else {
2049
    (*ppDst)->pFields = NULL;
×
2050
  }
2051
  
2052
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
914!
2053
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
574✔
2054
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
574!
2055
  } else {
2056
    (*ppDst)->pTagFields = NULL;
340✔
2057
  }
2058

2059
_exit:
914✔
2060

2061
  if (code != 0) {
914!
2062
    if (*ppDst) {
×
2063
      destroyStreamInserterParam(*ppDst);
×
2064
      *ppDst = NULL;
×
2065
    }
2066
    
2067
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2068
  }
2069
  return code;
914✔
2070
}
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