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

taosdata / TDengine / #4780

06 Oct 2025 08:05AM UTC coverage: 57.808% (-0.7%) from 58.554%
#4780

push

travis-ci

web-flow
Merge pull request #33175 from taosdata/docs/fix-download-url

fix: remove unnecessary jsonPath attribute and add  tdgpt download url

137100 of 302743 branches covered (45.29%)

Branch coverage included in aggregate %.

208096 of 294403 relevant lines covered (70.68%)

5550964.99 hits per line

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

54.27
/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 "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

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

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

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

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

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

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

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

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

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

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

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

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

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

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

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

166
  return 0;
49,190✔
167
}
168

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

174
  // set the idstr for tsdbReader
175
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
49,187✔
176
}
177

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

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

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

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

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

201
  return code;
×
202
}
203

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

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

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

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

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

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

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

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

252
  return pTaskInfo;
1,019✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
18,218✔
256
  if (streamInserterParam == NULL) {
18,218✔
257
    return TSDB_CODE_SUCCESS;
17,341✔
258
  }
259

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

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

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

276
  return TSDB_CODE_SUCCESS;
877✔
277
}
278

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

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

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

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

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

334
_error:
3,205✔
335

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

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

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

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

365
  *pTaskInfo = NULL;
18,223✔
366

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

382
  return code;
18,217✔
383
}
384

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

397
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
289✔
398

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

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

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

417
    tDecoderClear(&mr.coder);
343✔
418

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

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

445
      if (!qualified) {
159✔
446
        continue;
79✔
447
      }
448
    }
449

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

455
_end:
289✔
456

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

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

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

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

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

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

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

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

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

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

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

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

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

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

565
  return code;
296✔
566
}
567

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

573
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
2,780,989!
574
    return TSDB_CODE_INVALID_PARA;
×
575
  }
576
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,781,059✔
577

578
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
2,781,059✔
579
    return TSDB_CODE_SUCCESS;
1,614,829✔
580
  }
581

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

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

602
  *tbGet = true;
1,166,334✔
603

604
  return TSDB_CODE_SUCCESS;
1,166,334✔
605
}
606

607
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
1,614,904✔
608

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

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

621
int32_t qExecutorInit(void) {
14,977✔
622
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
14,977✔
623
  return TSDB_CODE_SUCCESS;
14,978✔
624
}
625

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

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

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

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

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

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

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

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

680
_error:
119,282✔
681
  // if failed to add ref for all tables in this query, abort current query
682
  return code;
1,622,761✔
683
}
684

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

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

697
  if (pLocal) {
1,748,914✔
698
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
1,731,502✔
699
  }
700

701
  taosArrayClear(pResList);
1,748,914✔
702

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

710
  if (pTaskInfo->cost.start == 0) {
1,749,014✔
711
    pTaskInfo->cost.start = taosGetTimestampUs();
1,606,187✔
712
  }
713

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

720
  // error occurs, record the error code and return to client
721
  int32_t ret = setjmp(pTaskInfo->env);
1,748,868✔
722
  if (ret != TSDB_CODE_SUCCESS) {
1,750,070✔
723
    pTaskInfo->code = ret;
1,174✔
724
    (void)cleanUpUdfs();
1,174✔
725

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

729
    return pTaskInfo->code;
1,174✔
730
  }
731

732
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
1,748,896✔
733

734
  int32_t      current = 0;
1,748,900✔
735
  SSDataBlock* pRes = NULL;
1,748,900✔
736
  int64_t      st = taosGetTimestampUs();
1,748,960✔
737

738
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
1,748,960!
739
    pTaskInfo->paramSet = true;
42,546✔
740
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
42,546✔
741
  } else {
742
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
1,706,414✔
743
  }
744

745
  QUERY_CHECK_CODE(code, lino, _end);
1,747,657!
746
  code = blockDataCheck(pRes);
1,747,657✔
747
  QUERY_CHECK_CODE(code, lino, _end);
1,747,718!
748

749
  if (pRes == NULL) {
1,747,718✔
750
    st = taosGetTimestampUs();
320,415✔
751
  }
752

753
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
1,747,721✔
754
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
1,747,721✔
755
    rowsThreshold = 4096;
1,738,418✔
756
  }
757

758
  int32_t blockIndex = 0;
1,747,721✔
759
  while (pRes != NULL) {
4,908,159✔
760
    SSDataBlock* p = NULL;
3,298,894✔
761
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
3,298,894✔
762
      SSDataBlock* p1 = NULL;
2,771,031✔
763
      code = createOneDataBlock(pRes, true, &p1);
2,771,031✔
764
      QUERY_CHECK_CODE(code, lino, _end);
2,770,800!
765

766
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
2,770,800✔
767
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,770,808!
768
      p = p1;
2,770,808✔
769
    } else {
770
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
528,207✔
771
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
527,978!
772

773
      p = *(SSDataBlock**)tmp;
527,978✔
774
      code = copyDataBlock(p, pRes);
527,978✔
775
      QUERY_CHECK_CODE(code, lino, _end);
529,504!
776
    }
777

778
    blockIndex += 1;
3,300,312✔
779

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

786
    if (current >= rowsThreshold || processOneBlock) {
3,300,002✔
787
      break;
788
    }
789

790
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
3,161,503✔
791
    QUERY_CHECK_CODE(code, lino, _end);
3,161,268!
792
    code = blockDataCheck(pRes);
3,161,268✔
793
    QUERY_CHECK_CODE(code, lino, _end);
3,160,438!
794
  }
795

796
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
1,747,764✔
797
    pTaskInfo->pSubplan->rowsThreshold -= current;
9,338✔
798
  }
799

800
  *hasMore = (pRes != NULL);
1,747,764✔
801
  uint64_t el = (taosGetTimestampUs() - st);
1,747,751✔
802

803
  pTaskInfo->cost.elapsedTime += el;
1,747,751✔
804
  if (NULL == pRes) {
1,747,751✔
805
    *useconds = pTaskInfo->cost.elapsedTime;
1,609,156✔
806
  }
807

808
_end:
138,595✔
809
  (void)cleanUpUdfs();
1,747,751✔
810

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

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

821
  return pTaskInfo->code;
1,747,848✔
822
}
823

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

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

838
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
265,533✔
839
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
265,533✔
840
  int64_t        threadId = taosGetSelfPthreadId();
265,533✔
841
  int64_t        curOwner = 0;
265,532✔
842

843
  *pRes = NULL;
265,532✔
844

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

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

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

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

863
  pTaskInfo->owner = threadId;
265,540✔
864
  taosRUnLockLatch(&pTaskInfo->lock);
265,540✔
865

866
  if (pTaskInfo->cost.start == 0) {
265,540✔
867
    pTaskInfo->cost.start = taosGetTimestampUs();
711✔
868
  }
869

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

880
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
265,541!
881

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

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

901
  uint64_t el = (taosGetTimestampUs() - st);
265,513✔
902

903
  pTaskInfo->cost.elapsedTime += el;
265,513✔
904
  if (NULL == *pRes) {
265,513✔
905
    *useconds = pTaskInfo->cost.elapsedTime;
30,675✔
906
  }
907

908
  (void)cleanUpUdfs();
265,513✔
909

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

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

916
  atomic_store_64(&pTaskInfo->owner, 0);
265,538✔
917
  return pTaskInfo->code;
265,540✔
918
}
919

920
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
585,631✔
921
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
585,631✔
922
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
585,639✔
923
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
585,638✔
924

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

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

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

942
  return 0;
×
943
}
944

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

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

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

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

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

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

990
  setTaskKilled(pTaskInfo, rspCode);
1,344✔
991
  qStopTaskOperators(pTaskInfo);
1,344✔
992

993
  return TSDB_CODE_SUCCESS;
1,344✔
994
}
995

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

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

1009
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1010

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

1017
        taosMsleep(200);
×
1018

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

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

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

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

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

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

1068
void qDestroyTask(qTaskInfo_t qTaskHandle) {
1,656,848✔
1069
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
1,656,848✔
1070
  if (pTaskInfo == NULL) {
1,656,848✔
1071
    return;
15,363✔
1072
  }
1073

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

1080
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
1,641,488✔
1081
  doDestroyTask(pTaskInfo);
1,641,486✔
1082
}
1083

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

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

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

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

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

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

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

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

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

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

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

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

1173
    pCond->pSlotList[i] = i;
13,316✔
1174
  }
1175

1176
  return TSDB_CODE_SUCCESS;
2,513✔
1177
}
1178

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

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

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

1195
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
33,652✔
1196
  const char*    id = GET_TASKID(pTaskInfo);
33,652✔
1197

1198
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
33,652✔
1199
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
29,459✔
1200
    if (pOperator == NULL || code != 0) {
29,459!
1201
      return code;
×
1202
    }
1203

1204
    SStreamScanInfo* pInfo = pOperator->info;
29,459✔
1205
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
29,459✔
1206
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
29,459✔
1207
    walReaderVerifyOffset(pWalReader, pOffset);
29,459✔
1208
  }
1209
  // if pOffset equal to current offset, means continue consume
1210
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
33,652✔
1211
    return 0;
28,672✔
1212
  }
1213

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1453
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
972,187✔
1454

1455
  if (pMsg->contLen > 0) {
972,187✔
1456
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
972,109!
1457
    if (buf.pData == NULL) {
972,148!
1458
      pMsg->code = terrno;
×
1459
    } else {
1460
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
972,148✔
1461
    }
1462
  }
1463

1464
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
972,226✔
1465
  rpcFreeCont(pMsg->pCont);
972,309✔
1466
  destroySendMsgInfo(pSendInfo);
972,269✔
1467
}
1468

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

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

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

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

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

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

1497
  taosArrayDestroy(plist);
×
1498

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1591
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
144,986✔
1592
  int32_t code = 0;
144,986✔
1593

1594
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
144,986✔
1595
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
144,974✔
1596

1597
  if (pOper->fpSet.resetStateFn) {
144,970!
1598
    code = pOper->fpSet.resetStateFn(pOper);
144,973✔
1599
  }
1600
  pOper->status = OP_NOT_OPENED;
144,971✔
1601
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
251,773!
1602
    code = clearStatesForOperator(pOper->pDownstream[i]);
106,786✔
1603
  }
1604
  return code;
144,987✔
1605
}
1606

1607
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
38,215✔
1608
  int32_t        code = 0;
38,215✔
1609
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
38,215✔
1610
  SOperatorInfo* pOper = pTaskInfo->pRoot;
38,215✔
1611
  pTaskInfo->code = TSDB_CODE_SUCCESS;
38,215✔
1612
  code = clearStatesForOperator(pOper);
38,215✔
1613
  return code;
38,213✔
1614
}
1615

1616
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
49,591✔
1617
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
49,591✔
1618
  int64_t        threadId = taosGetSelfPthreadId();
49,591✔
1619
  int64_t        curOwner = 0;
49,592✔
1620

1621
  *ppRes = NULL;
49,592✔
1622

1623
  // todo extract method
1624
  taosRLockLatch(&pTaskInfo->lock);
49,592✔
1625
  bool isKilled = isTaskKilled(pTaskInfo);
49,601✔
1626
  if (isKilled) {
49,598!
1627
    // clearStreamBlock(pTaskInfo->pRoot);
1628
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1629

1630
    taosRUnLockLatch(&pTaskInfo->lock);
×
1631
    return pTaskInfo->code;
×
1632
  }
1633

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

1638
    taosRUnLockLatch(&pTaskInfo->lock);
×
1639
    return pTaskInfo->code;
×
1640
  }
1641

1642
  pTaskInfo->owner = threadId;
49,598✔
1643
  taosRUnLockLatch(&pTaskInfo->lock);
49,598✔
1644

1645
  if (pTaskInfo->cost.start == 0) {
49,599✔
1646
    pTaskInfo->cost.start = taosGetTimestampUs();
893✔
1647
  }
1648

1649
  // error occurs, record the error code and return to client
1650
  int32_t ret = setjmp(pTaskInfo->env);
49,599✔
1651
  if (ret != TSDB_CODE_SUCCESS) {
63,755✔
1652
    pTaskInfo->code = ret;
14,156✔
1653
    (void)cleanUpUdfs();
14,156✔
1654
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
14,156✔
1655
    atomic_store_64(&pTaskInfo->owner, 0);
14,156✔
1656
    return pTaskInfo->code;
14,156✔
1657
  }
1658

1659
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
49,599✔
1660

1661
  int64_t st = taosGetTimestampUs();
49,595✔
1662

1663
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
49,595✔
1664
  if (code) {
35,431!
1665
    pTaskInfo->code = code;
×
1666
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1667
  } else {
1668
    *finished = *ppRes == NULL;
35,431✔
1669
    code = blockDataCheck(*ppRes);
35,431✔
1670
  }
1671
  if (code) {
35,437!
1672
    pTaskInfo->code = code;
×
1673
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1674
  }
1675

1676
  uint64_t el = (taosGetTimestampUs() - st);
35,437✔
1677

1678
  pTaskInfo->cost.elapsedTime += el;
35,437✔
1679
  if (NULL == *ppRes) {
35,437✔
1680
    *useconds = pTaskInfo->cost.elapsedTime;
23,311✔
1681
  }
1682

1683
  (void)cleanUpUdfs();
35,437✔
1684

1685
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
35,446✔
1686
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
35,446✔
1687

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

1691
  atomic_store_64(&pTaskInfo->owner, 0);
35,446✔
1692
  return pTaskInfo->code;
35,446✔
1693
}
1694

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

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

1709
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
12,711✔
1710
  SReadHandle    pHandle = {.vnode = pVnode};
12,711✔
1711
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
12,711✔
1712

1713
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
12,711✔
1714
  if (code != 0) {
12,769✔
1715
    tableListDestroy(pList);
2✔
1716
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1717
    return code;
×
1718
  }
1719
  *pTableListInfo = pList;
12,767✔
1720
  return 0;
12,767✔
1721
}
1722

1723
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
33,057✔
1724
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
33,057!
1725
    return TSDB_CODE_INVALID_PARA;
×
1726
  }
1727
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
33,064✔
1728
    *size = 0;
22,920✔
1729
    *pKeyInfo = NULL;
22,920✔
1730
    return 0;
22,920✔
1731
  }
1732
  if (currentGroupId == -1) {
10,143✔
1733
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
896✔
1734
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
896✔
1735
    return 0;
896✔
1736
  }
1737
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
9,247✔
1738
}
1739

1740
int32_t  qStreamSetTableList(void** pTableListInfo, uint64_t uid, uint64_t gid){
7,100✔
1741
  if (*pTableListInfo == NULL) {
7,100✔
1742
    *pTableListInfo = tableListCreate();
1,190✔
1743
    if (*pTableListInfo == NULL) {
1,188!
1744
      return terrno;
×
1745
    }
1746
  }
1747
  return tableListAddTableInfo(*pTableListInfo, uid, gid);
7,098✔
1748
}
1749

1750
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
28,276✔
1751
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
28,276✔
1752
    return 0;
22,705✔
1753
  }
1754
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
8,222✔
1755
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
7,884✔
1756

1757
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
7,884✔
1758
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
7,882!
1759
      return i;
5,231✔
1760
    }
1761
  }
1762
  return -1;
338✔
1763
}
1764

1765
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
14,849✔
1766

1767
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
15,032✔
1768

1769
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
4,704✔
1770
void    qStreamSetTableListGroupNum(const void* pTableList, int32_t groupNum) {((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; }
1,191✔
1771
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
708✔
1772

1773
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
4,253✔
1774

1775
void streamDestroyExecTask(qTaskInfo_t tInfo) {
7,234✔
1776
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
7,234✔
1777
  qDestroyTask(tInfo);
7,234✔
1778
}
7,235✔
1779

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

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

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

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

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

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

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

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

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

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

1986
    memcpy(tbname, pVal, len);
804✔
1987
    tbname[len] = '\0';  // ensure null terminated
804✔
1988
    if (NULL != strchr(tbname, '.')) {
804!
1989
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
1990
      qError("tbname generated with invalid characters, '.' is not allowed");
×
1991
    }
1992
  }
1993
  // TODO free dst
1994
  sclFreeParam(&dst);
804✔
1995
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
804✔
1996
  return code;
804✔
1997
}
1998

1999
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
877✔
2000
  if (pParam) {
877!
2001
    if (pParam->tbname) {
877!
2002
      taosMemFree(pParam->tbname);
877✔
2003
      pParam->tbname = NULL;
877✔
2004
    }
2005
    if (pParam->stbname) {
877!
2006
      taosMemFree(pParam->stbname);
877✔
2007
      pParam->stbname = NULL;
877✔
2008
    }
2009
    if (pParam->dbFName) {
877!
2010
      taosMemFree(pParam->dbFName);
877✔
2011
      pParam->dbFName = NULL;
877✔
2012
    }
2013
    if (pParam->pFields) {
877!
2014
      taosArrayDestroy(pParam->pFields);
877✔
2015
      pParam->pFields = NULL;
877✔
2016
    }
2017
    if (pParam->pTagFields) {
877✔
2018
      taosArrayDestroy(pParam->pTagFields);
584✔
2019
      pParam->pTagFields = NULL;
584✔
2020
    }
2021
    taosMemFree(pParam);
877✔
2022
  }
2023
}
877✔
2024

2025
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
877✔
2026
  int32_t code = 0, lino = 0;
877✔
2027
  if (ppDst == NULL || pSrc == NULL) {
877!
2028
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2029
  }
2030
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
877!
2031
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
876!
2032

2033
  (*ppDst)->suid = pSrc->suid;
876✔
2034
  (*ppDst)->sver = pSrc->sver;
876✔
2035
  (*ppDst)->tbType = pSrc->tbType;
876✔
2036
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
876!
2037
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
875!
2038

2039
  if (pSrc->stbname) {
875!
2040
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
875!
2041
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
876!
2042
  }
2043

2044
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
876!
2045
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
877!
2046

2047
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
877✔
2048

2049
  if (pSrc->pFields && pSrc->pFields->size > 0) {
877!
2050
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
876✔
2051
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
877!
2052
  } else {
2053
    (*ppDst)->pFields = NULL;
1✔
2054
  }
2055
  
2056
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
878!
2057
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
584✔
2058
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
584!
2059
  } else {
2060
    (*ppDst)->pTagFields = NULL;
294✔
2061
  }
2062

2063
_exit:
878✔
2064

2065
  if (code != 0) {
878!
2066
    if (*ppDst) {
×
2067
      destroyStreamInserterParam(*ppDst);
×
2068
      *ppDst = NULL;
×
2069
    }
2070
    
2071
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2072
  }
2073
  return code;
877✔
2074
}
2075

2076
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
1✔
2077
  return doDropStreamTable(pMsgCb, pOutput, pReq);
1✔
2078
}
2079

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