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

taosdata / TDengine / #4971

28 Feb 2026 08:05AM UTC coverage: 67.671% (-0.04%) from 67.707%
#4971

push

travis-ci

web-flow
fix(planner): disable project block merge in non-top-level subplans (#34617)

208281 of 307783 relevant lines covered (67.67%)

130135765.28 hits per line

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

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

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

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

44
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
584,228,669✔
45
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
584,228,669✔
46
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
584,228,669✔
47
  gTaskScalarExtra.fp = qFetchRemoteNode;
584,255,441✔
48
}
584,176,899✔
49

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

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

66
static void cleanupRefPool() {
550,531✔
67
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
550,531✔
68
  taosCloseRef(ref);
550,531✔
69
}
550,531✔
70

71
static void initRefPool() {
550,531✔
72
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
550,531✔
73
  (void)atexit(cleanupRefPool);
550,531✔
74
}
550,531✔
75

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

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

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

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

129
    return TSDB_CODE_SUCCESS;
×
130
  }
131

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

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

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

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

157
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
49,584,825✔
158
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
49,584,825✔
159
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
49,585,862✔
160
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
22,587,820✔
161
    if (pStreamScanInfo->pTableScanOp != NULL) {
22,587,529✔
162
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
22,587,529✔
163
      if (pScanInfo->base.dataReader != NULL) {
22,586,830✔
164
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
268,954✔
165
        if (code) {
268,638✔
166
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
167
          return code;
×
168
        }
169
      }
170
    }
171
  } else {
172
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
26,996,448✔
173
  }
174

175
  return 0;
26,346,094✔
176
}
177

178
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
26,343,664✔
179
  SExecTaskInfo* pTaskInfo = tinfo;
26,343,664✔
180
  pTaskInfo->id.queryId = queryId;
26,343,664✔
181
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
26,344,693✔
182

183
  // set the idstr for tsdbReader
184
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
26,346,753✔
185
}
186

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

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

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

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

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

210
  return code;
×
211
}
212

213
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
452,059✔
214
  if (msg == NULL) {  // create raw scan
452,059✔
215
    SExecTaskInfo* pTaskInfo = NULL;
122,245✔
216

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

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

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

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

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

249
  return pTaskInfo;
330,134✔
250
}
251

252
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
680,393✔
253
  if (streamInserterParam == NULL) {
680,393✔
254
    return TSDB_CODE_SUCCESS;
423,709✔
255
  }
256

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

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

267
  if (streamInserterParam->suid <= 0 &&
256,684✔
268
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
103,934✔
269
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
270
    return TSDB_CODE_INVALID_PARA;
×
271
  }
272

273
  return TSDB_CODE_SUCCESS;
256,684✔
274
}
275

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

296
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, NULL);
680,393✔
297
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
680,393✔
298
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
335✔
299
    goto _error;
223✔
300
  }
301

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

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

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

331
_error:
72,644✔
332

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

342
bool qNeedReset(qTaskInfo_t pInfo) {
3,078,407✔
343
  if (pInfo == NULL) {
3,078,407✔
344
    return false;
×
345
  }
346
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,078,407✔
347
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,078,407✔
348
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,078,407✔
349
    return false;
×
350
  }
351
  int32_t node = nodeType(pOperator->pPhyNode);
3,078,407✔
352
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,798,538✔
353
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
527,474✔
354
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,876,945✔
355
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
356
}
357

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

363
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,550,685✔
364
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,550,685✔
365
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,550,685✔
366
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,550,685✔
367
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,550,933✔
368
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,550,933✔
369
}
370

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

378
  void*           info = pOperator->info;
3,078,407✔
379
  STableScanBase* pScanBaseInfo = NULL;
3,078,407✔
380

381
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,078,407✔
382
    pScanBaseInfo = &((STableScanInfo*)info)->base;
279,869✔
383
    setReadHandle(handle, pScanBaseInfo);
279,869✔
384
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,798,290✔
385
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,270,816✔
386
    setReadHandle(handle, pScanBaseInfo);
2,270,816✔
387
  }
388

389
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
3,078,159✔
390
  handle->winRange.ekey);
391
  return pOperator->fpSet.resetStateFn(pOperator);
3,078,407✔
392
}
393

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

400
  *pTaskInfo = NULL;
680,393✔
401

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

417
  return code;
680,170✔
418
}
419

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

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

433
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
420,822✔
434

435
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
420,822✔
436
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
420,822✔
437

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

444
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
420,822✔
445

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

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

455
  locked = 1;
420,822✔
456
  for (int32_t i = 0; i < numOfUids; ++i) {
1,170,999✔
457
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
750,177✔
458
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
750,177✔
459

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

466
    tDecoderClear(&mr.coder);
750,177✔
467

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

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

495
  pAPI->metaReaderFn.clearReader(&mr);
420,822✔
496
  locked = 0;
420,822✔
497

498
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
1,167,439✔
499
    bool     qualified = false;
746,617✔
500
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
746,617✔
501
    if (t == NULL) {
746,617✔
502
      continue;
×
503
    }
504

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

512
      if (!qualified) {
343,461✔
513
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
171,593✔
514
        continue;
171,593✔
515
      }
516
    }
517

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

522
  // handle multiple partition
523

524
_end:
420,822✔
525

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

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

539
int32_t qDeleteTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
1,416✔
540
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,416✔
541
  const char*    id = GET_TASKID(pTaskInfo);
1,416✔
542
  int32_t        code = 0;
1,416✔
543

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

551
  SStreamScanInfo* pScanInfo = pInfo->info;
1,416✔
552
  qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
1,416✔
553
  taosWLockLatch(&pTaskInfo->lock);
1,416✔
554
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,416✔
555
  taosWUnLockLatch(&pTaskInfo->lock);
1,416✔
556

557
  return code;
1,416✔
558
}
559

560
static int32_t filterTableForTmqQuery(SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* id, SStorageAPI* pAPI, SRWLatch* lock) {
420,822✔
561
  SArray* qa = NULL;
420,822✔
562
  int32_t code = filterUnqualifiedTables(pScanInfo, tableIdList, id, pAPI, &qa);
420,822✔
563
  if (code != TSDB_CODE_SUCCESS) {
420,822✔
564
    taosArrayDestroy(qa);
×
565
    return code;
×
566
  }
567
  int32_t numOfQualifiedTables = taosArrayGetSize(qa);
420,822✔
568
  qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
420,822✔
569
  pAPI->tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
420,822✔
570

571
  bool   assignUid = false;
420,822✔
572
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
420,822✔
573
  char*  keyBuf = NULL;
420,822✔
574
  if (bufLen > 0) {
420,822✔
575
    assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
576
    keyBuf = taosMemoryMalloc(bufLen);
×
577
    if (keyBuf == NULL) {
×
578
      taosArrayDestroy(qa);
×
579
      return terrno;
×
580
    }
581
  }
582

583
  STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
420,822✔
584
  taosWLockLatch(lock);
420,822✔
585

586
  for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
995,846✔
587
    uint64_t* uid = taosArrayGet(qa, i);
575,024✔
588
    if (!uid) {
575,024✔
589
      taosMemoryFree(keyBuf);
×
590
      taosArrayDestroy(qa);
×
591
      taosWUnLockLatch(lock);
×
592
      return terrno;
×
593
    }
594
    STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
575,024✔
595

596
    if (bufLen > 0) {
575,024✔
597
      if (assignUid) {
×
598
        keyInfo.groupId = keyInfo.uid;
×
599
      } else {
600
        code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
601
                                      &keyInfo.groupId, pAPI);
602
        if (code != TSDB_CODE_SUCCESS) {
×
603
          taosMemoryFree(keyBuf);
×
604
          taosArrayDestroy(qa);
×
605
          taosWUnLockLatch(lock);
×
606
          return code;
×
607
        }
608
      }
609
    }
610

611
    code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
575,024✔
612
    if (code != TSDB_CODE_SUCCESS) {
575,024✔
613
      taosMemoryFree(keyBuf);
×
614
      taosArrayDestroy(qa);
×
615
      taosWUnLockLatch(lock);
×
616
      return code;
×
617
    }
618
  }
619

620
  taosWUnLockLatch(lock);
420,822✔
621
  if (keyBuf != NULL) {
420,822✔
622
    taosMemoryFree(keyBuf);
×
623
  }
624

625
  taosArrayDestroy(qa);
420,822✔
626
  return 0;
420,822✔
627
}
628

629
static void qUpdateTableTagCache(SStreamScanInfo* pScanInfo, const SArray* tableIdList, col_id_t cid, SStorageAPI* api) {
421,475✔
630
  STqReader*   tqReader = pScanInfo->tqReader;
421,475✔
631
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
1,172,305✔
632
    int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
750,830✔
633
    api->tqReaderFn.tqUpdateTableTagCache(pScanInfo->tqReader, pScanInfo->pPseudoExpr, pScanInfo->numOfPseudoExpr, *uid, cid);
750,830✔
634
  }
635
}
421,475✔
636

637
int32_t qAddTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
420,822✔
638
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
420,822✔
639
  const char*    id = GET_TASKID(pTaskInfo);
420,822✔
640
  int32_t        code = 0;
420,822✔
641

642
  qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
420,822✔
643

644
  // traverse to the stream scanner node to add this table id
645
  SOperatorInfo* pInfo = NULL;
420,822✔
646
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
420,822✔
647
  if (code != 0 || pInfo == NULL) {
420,822✔
648
    return code;
×
649
  }
650

651
  SStreamScanInfo* pScanInfo = pInfo->info;
420,822✔
652
  qUpdateTableTagCache(pScanInfo, tableIdList, 0, &pTaskInfo->storageAPI);
420,822✔
653

654
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
420,822✔
655
}
656

657
void qUpdateTableTagCacheForTmq(qTaskInfo_t tinfo, const SArray* tableIdList, SArray* cids) {
653✔
658
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
653✔
659
  const char*    id = GET_TASKID(pTaskInfo);
653✔
660
  int32_t        code = 0;
653✔
661

662
  // traverse to the stream scanner node to add this table id
663
  SOperatorInfo* pInfo = NULL;
653✔
664
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
653✔
665
  if (code != 0 || pInfo == NULL) {
653✔
666
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
667
    return;
×
668
  }
669

670
  SStreamScanInfo* pScanInfo = pInfo->info;
653✔
671
  for (int32_t i = 0; i < taosArrayGetSize(cids); ++i) {
1,306✔
672
    col_id_t* cid = (col_id_t*)taosArrayGet(cids, i);
653✔
673
    qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
653✔
674
  }
675
}
676

677
int32_t qUpdateTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
678
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
679
  const char*    id = GET_TASKID(pTaskInfo);
×
680
  int32_t        code = 0;
×
681

682
  // traverse to the stream scanner node to add this table id
683
  SOperatorInfo* pInfo = NULL;
×
684
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
×
685
  if (code != 0 || pInfo == NULL) {
×
686
    return code;
×
687
  }
688

689
  SStreamScanInfo* pScanInfo = pInfo->info;
×
690

691
  qDebug("%s %d remove child tables from the stream scanner, %s", __func__, (int32_t)taosArrayGetSize(tableIdList), id);
×
692
  taosWLockLatch(&pTaskInfo->lock);
×
693
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
694
  taosWUnLockLatch(&pTaskInfo->lock);
×
695
  
696
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
×
697
}
698

699
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
456,119,515✔
700
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
701
                                    int32_t idx, bool* tbGet) {
702
  *tbGet = false;
456,119,515✔
703

704
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
456,211,518✔
705
    return TSDB_CODE_INVALID_PARA;
45✔
706
  }
707
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
456,300,524✔
708

709
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
456,300,524✔
710
    return TSDB_CODE_SUCCESS;
267,453,812✔
711
  }
712

713
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
188,820,992✔
714
  if (!pSchemaInfo) {
188,737,122✔
715
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
716
    return terrno;
×
717
  }
718

719
  *sversion = pSchemaInfo->sw->version;
188,737,122✔
720
  *tversion = pSchemaInfo->tversion;
188,907,957✔
721
  *rversion = pSchemaInfo->rversion;
188,838,241✔
722
  if (pSchemaInfo->dbname) {
188,803,172✔
723
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
188,767,859✔
724
  } else {
725
    dbName[0] = 0;
×
726
  }
727
  if (pSchemaInfo->tablename) {
188,978,403✔
728
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
188,806,751✔
729
  } else {
730
    tableName[0] = 0;
41,807✔
731
  }
732

733
  *tbGet = true;
188,905,130✔
734

735
  return TSDB_CODE_SUCCESS;
188,882,883✔
736
}
737

738
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
267,435,640✔
739

740
void qDestroyOperatorParam(SOperatorParam* pParam) {
122,112✔
741
  if (NULL == pParam) {
122,112✔
742
    return;
×
743
  }
744
  freeOperatorParam(pParam, OP_GET_PARAM);
122,112✔
745
}
746

747
/**
748
  @brief Update the operator param for the task.
749
  @note  Unlike setOperatorParam, this function will destroy the new param when
750
         operator type mismatch.
751
*/
752
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
32,274,600✔
753
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
32,274,600✔
754
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
32,274,600✔
755
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
32,274,600✔
756
    qError("%s, %s operator type mismatch, task operator type:%d, "
117,342✔
757
           "new param operator type:%d", GET_TASKID(pTask), __func__,
758
           pTask->pRoot->operatorType,
759
           pNewParam->opType);
760
    qDestroyOperatorParam((SOperatorParam*)pParam);
117,342✔
761
    return;
117,342✔
762
  }
763
  TSWAP(pParam, pTask->pOpParam);
32,162,449✔
764
  ((SExecTaskInfo*)tinfo)->paramSet = false;
32,158,517✔
765
}
766

767
int32_t qExecutorInit(void) {
4,337,183✔
768
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,337,183✔
769
  return TSDB_CODE_SUCCESS;
4,338,080✔
770
}
771

772
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
2,847,782✔
773
  int32_t        code = TSDB_CODE_SUCCESS;
2,847,782✔
774
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
2,847,782✔
775
  if (pTask->pWorkerCb) {
2,847,782✔
776
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
2,844,086✔
777
    if (code != TSDB_CODE_SUCCESS) {
2,852,006✔
778
      pTask->code = code;
×
779
      return pTask->code;
×
780
    }
781
  }
782

783
  code = tsem_wait(pSem);
2,859,926✔
784
  if (code != TSDB_CODE_SUCCESS) {
2,851,478✔
785
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
786
    pTask->code = code;
×
787
    return pTask->code;
×
788
  }
789

790
  if (pTask->pWorkerCb) {
2,851,478✔
791
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,852,006✔
792
    if (code != TSDB_CODE_SUCCESS) {
2,852,006✔
793
      pTask->code = code;
×
794
      return pTask->code;
×
795
    }
796
  }
797
  return TSDB_CODE_SUCCESS;
2,852,006✔
798
}
799

800
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
269,910,347✔
801
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
802
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
803
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
269,910,347✔
804
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
269,910,347✔
805

806
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
269,924,096✔
807
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
808

809
  readHandle->uid = 0;
269,973,263✔
810
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
270,000,272✔
811
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
269,678,875✔
812
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
826,207✔
813
    goto _error;
450,096✔
814
  }
815
    
816
  if (handle) {
268,926,620✔
817
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
268,953,740✔
818
    void*           pSinkManager = NULL;
269,000,376✔
819
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
268,660,360✔
820
    if (code != TSDB_CODE_SUCCESS) {
268,555,829✔
821
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
822
      goto _error;
×
823
    }
824

825
    void* pSinkParam = NULL;
268,555,829✔
826
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
268,616,464✔
827
    if (code != TSDB_CODE_SUCCESS) {
268,547,401✔
828
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
829
      taosMemoryFree(pSinkManager);
×
830
      goto _error;
×
831
    }
832

833
    SDataSinkNode* pSink = NULL;
268,547,401✔
834
    if (readHandle->localExec) {
268,716,111✔
835
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,860✔
836
      if (code != TSDB_CODE_SUCCESS) {
2,860✔
837
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
12✔
838
               (*pTask)->id.str);
839
        taosMemoryFree(pSinkManager);
12✔
840
        goto _error;
×
841
      }
842
    }
843

844
    // pSinkParam has been freed during create sinker.
845
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
268,589,935✔
846
                              (*pTask)->id.str, pSubplan->processOneBlock);
268,908,546✔
847
    if (code) {
268,509,141✔
848
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
583✔
849
    }
850
  }
851

852
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
269,111,091✔
853
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
854

855
_error:
51,837,529✔
856
  // if failed to add ref for all tables in this query, abort current query
857
  return code;
269,803,369✔
858
}
859

860
static void freeBlock(void* param) {
×
861
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
862
  blockDataDestroy(pBlock);
×
863
}
×
864

865
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
317,297,704✔
866
                     bool processOneBlock) {
867
  int32_t        code = TSDB_CODE_SUCCESS;
317,297,704✔
868
  int32_t        lino = 0;
317,297,704✔
869
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
317,297,704✔
870
  int64_t        threadId = taosGetSelfPthreadId();
317,297,704✔
871

872
  if (pLocal) {
317,240,859✔
873
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
313,665,744✔
874
  }
875

876
  taosArrayClear(pResList);
317,076,259✔
877

878
  int64_t curOwner = 0;
317,161,311✔
879
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
317,161,311✔
880
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
881
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
882
    return pTaskInfo->code;
×
883
  }
884

885
  if (pTaskInfo->cost.start == 0) {
317,159,355✔
886
    pTaskInfo->cost.start = taosGetTimestampUs();
263,757,152✔
887
  }
888

889
  if (isTaskKilled(pTaskInfo)) {
317,208,498✔
890
    atomic_store_64(&pTaskInfo->owner, 0);
454✔
891
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
454✔
892
    return pTaskInfo->code;
454✔
893
  }
894

895
  // error occurs, record the error code and return to client
896
  int32_t ret = setjmp(pTaskInfo->env);
317,181,334✔
897
  if (ret != TSDB_CODE_SUCCESS) {
317,530,432✔
898
    pTaskInfo->code = ret;
573,888✔
899
    (void)cleanUpUdfs();
573,888✔
900

901
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
573,888✔
902
    atomic_store_64(&pTaskInfo->owner, 0);
573,888✔
903

904
    return pTaskInfo->code;
573,888✔
905
  }
906

907
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
316,956,544✔
908

909
  int32_t      current = 0;
316,972,778✔
910
  SSDataBlock* pRes = NULL;
316,972,778✔
911
  int64_t      st = taosGetTimestampUs();
317,225,195✔
912

913
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
317,225,195✔
914
    pTaskInfo->paramSet = true;
32,152,320✔
915
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
32,150,309✔
916
  } else {
917
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
285,112,679✔
918
  }
919

920
  QUERY_CHECK_CODE(code, lino, _end);
316,735,328✔
921
  code = blockDataCheck(pRes);
316,735,328✔
922
  QUERY_CHECK_CODE(code, lino, _end);
316,753,481✔
923

924
  if (pRes == NULL) {
316,753,481✔
925
    st = taosGetTimestampUs();
61,077,513✔
926
  }
927

928
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
316,752,417✔
929
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
316,747,585✔
930
    rowsThreshold = 4096;
316,179,352✔
931
  }
932

933
  int32_t blockIndex = 0;
316,696,382✔
934
  while (pRes != NULL) {
951,242,572✔
935
    SSDataBlock* p = NULL;
679,908,931✔
936
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
679,811,169✔
937
      SSDataBlock* p1 = NULL;
546,590,604✔
938
      code = createOneDataBlock(pRes, true, &p1);
546,598,510✔
939
      QUERY_CHECK_CODE(code, lino, _end);
546,562,320✔
940

941
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
546,562,320✔
942
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
546,635,118✔
943
      p = p1;
546,635,118✔
944
    } else if (processOneBlock) {
133,377,360✔
945
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
16,500,929✔
946
      if (tmp) {
16,500,929✔
947
        blockDataDestroy(*tmp);
16,500,929✔
948
        *tmp = NULL;
16,500,929✔
949
      }
950
      SSDataBlock* p1 = NULL;
16,500,929✔
951
      code = createOneDataBlock(pRes, true, &p1);
16,500,929✔
952
      QUERY_CHECK_CODE(code, lino, _end);
16,500,929✔
953

954
      *tmp = p1;
16,500,929✔
955
      p = p1;
16,500,929✔
956
    } else {
957
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
116,876,431✔
958
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
116,875,258✔
959

960
      p = *(SSDataBlock**)tmp;
116,875,258✔
961
      code = copyDataBlock(p, pRes);
116,874,982✔
962
      QUERY_CHECK_CODE(code, lino, _end);
116,874,120✔
963
    }
964

965
    blockIndex += 1;
680,012,045✔
966

967
    current += p->info.rows;
680,012,045✔
968
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
679,990,807✔
969
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
970
    void* tmp = taosArrayPush(pResList, &p);
680,008,772✔
971
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
680,008,772✔
972

973
    if (current >= rowsThreshold || processOneBlock) {
680,008,772✔
974
      break;
975
    }
976

977
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
634,569,078✔
978
    QUERY_CHECK_CODE(code, lino, _end);
634,473,074✔
979
    code = blockDataCheck(pRes);
634,473,074✔
980
    QUERY_CHECK_CODE(code, lino, _end);
634,587,386✔
981
  }
982

983
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
316,772,768✔
984
    pTaskInfo->pSubplan->rowsThreshold -= current;
528,862✔
985
  }
986

987
  *hasMore = (pRes != NULL);
316,781,277✔
988
  uint64_t el = (taosGetTimestampUs() - st);
316,768,241✔
989

990
  pTaskInfo->cost.elapsedTime += el;
316,768,241✔
991
  if (NULL == pRes) {
316,759,368✔
992
    *useconds = pTaskInfo->cost.elapsedTime;
271,319,665✔
993
  }
994

995
_end:
316,725,036✔
996
  (void)cleanUpUdfs();
316,755,439✔
997

998
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
316,831,943✔
999
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
316,831,943✔
1000
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
1001

1002
  atomic_store_64(&pTaskInfo->owner, 0);
316,831,943✔
1003
  if (code) {
316,829,903✔
1004
    pTaskInfo->code = code;
×
1005
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1006
  }
1007

1008
  return pTaskInfo->code;
316,829,903✔
1009
}
1010

1011
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
1012
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1013
  SArray*        pList = pTaskInfo->pResultBlockList;
×
1014
  size_t         num = taosArrayGetSize(pList);
×
1015
  for (int32_t i = 0; i < num; ++i) {
×
1016
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
1017
    if (p) {
×
1018
      blockDataDestroy(*p);
×
1019
    }
1020
  }
1021

1022
  taosArrayClear(pTaskInfo->pResultBlockList);
×
1023
}
×
1024

1025
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
23,934,544✔
1026
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,934,544✔
1027
  int64_t        threadId = taosGetSelfPthreadId();
23,934,544✔
1028
  int64_t        curOwner = 0;
23,934,900✔
1029

1030
  *pRes = NULL;
23,934,900✔
1031

1032
  // todo extract method
1033
  taosRLockLatch(&pTaskInfo->lock);
23,934,900✔
1034
  bool isKilled = isTaskKilled(pTaskInfo);
23,934,900✔
1035
  if (isKilled) {
23,934,900✔
1036
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1037

1038
    taosRUnLockLatch(&pTaskInfo->lock);
×
1039
    return pTaskInfo->code;
×
1040
  }
1041

1042
  if (pTaskInfo->owner != 0) {
23,934,900✔
1043
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1044
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1045

1046
    taosRUnLockLatch(&pTaskInfo->lock);
×
1047
    return pTaskInfo->code;
×
1048
  }
1049

1050
  pTaskInfo->owner = threadId;
23,934,900✔
1051
  taosRUnLockLatch(&pTaskInfo->lock);
23,934,900✔
1052

1053
  if (pTaskInfo->cost.start == 0) {
23,934,900✔
1054
    pTaskInfo->cost.start = taosGetTimestampUs();
233,113✔
1055
  }
1056

1057
  // error occurs, record the error code and return to client
1058
  int32_t ret = setjmp(pTaskInfo->env);
23,934,900✔
1059
  if (ret != TSDB_CODE_SUCCESS) {
23,934,271✔
1060
    pTaskInfo->code = ret;
×
1061
    (void)cleanUpUdfs();
×
1062
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
1063
    atomic_store_64(&pTaskInfo->owner, 0);
×
1064
    return pTaskInfo->code;
×
1065
  }
1066

1067
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
23,934,271✔
1068

1069
  int64_t st = taosGetTimestampUs();
23,934,900✔
1070
  int32_t code = TSDB_CODE_SUCCESS;
23,934,900✔
1071
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
23,934,900✔
1072
    pTaskInfo->paramSet = true;
×
1073
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1074
  } else {
1075
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
23,934,900✔
1076
  }
1077
  if (code) {
23,933,706✔
1078
    pTaskInfo->code = code;
×
1079
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1080
  }
1081

1082
  code = blockDataCheck(*pRes);
23,933,706✔
1083
  if (code) {
23,934,235✔
1084
    pTaskInfo->code = code;
×
1085
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1086
  }
1087

1088
  uint64_t el = (taosGetTimestampUs() - st);
23,934,609✔
1089

1090
  pTaskInfo->cost.elapsedTime += el;
23,934,609✔
1091
  if (NULL == *pRes) {
23,934,266✔
1092
    *useconds = pTaskInfo->cost.elapsedTime;
10,873,288✔
1093
  }
1094

1095
  (void)cleanUpUdfs();
23,934,609✔
1096

1097
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
23,934,634✔
1098
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
23,934,900✔
1099

1100
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
23,934,900✔
1101
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1102

1103
  atomic_store_64(&pTaskInfo->owner, 0);
23,934,900✔
1104
  return pTaskInfo->code;
23,934,900✔
1105
}
1106

1107
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
101,780,429✔
1108
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
101,780,429✔
1109
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
101,781,925✔
1110
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
101,782,781✔
1111

1112
  if (!tmp) {
101,780,042✔
1113
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1114
    return terrno;
×
1115
  }
1116
  return TSDB_CODE_SUCCESS;
101,780,042✔
1117
}
1118

1119
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1120
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1121
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1122

1123
  if (key->refId < pInfo->refId) {
×
1124
    return -1;
×
1125
  } else if (key->refId > pInfo->refId) {
×
1126
    return 1;
×
1127
  }
1128

1129
  return 0;
×
1130
}
1131

1132
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1133
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1134
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1135
  if (idx >= 0) {
×
1136
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1137
  }
1138
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1139
}
×
1140

1141
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
57,065✔
1142
  if (pTaskInfo->subJobCtx.hasSubJobs) {
57,065✔
1143
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
15,840✔
1144
    if (pTaskInfo->subJobCtx.param) {
15,840✔
1145
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
7,920✔
1146
    }
1147
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
15,840✔
1148
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
15,840✔
1149
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
15,840✔
1150
  }
1151
  
1152
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
57,065✔
1153

1154
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
57,065✔
1155
  for (int32_t i = 0; i < num; ++i) {
60,547✔
1156
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
3,482✔
1157
    if (!pStop) {
3,482✔
1158
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1159
      continue;
×
1160
    }
1161
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
3,482✔
1162
    if (pExchangeInfo) {
3,482✔
1163
      int32_t code = tsem_post(&pExchangeInfo->ready);
3,482✔
1164
      if (code != TSDB_CODE_SUCCESS) {
3,482✔
1165
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1166
      } else {
1167
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
3,482✔
1168
      }
1169
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
3,482✔
1170
      if (code != TSDB_CODE_SUCCESS) {
3,482✔
1171
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1172
      }
1173
    }
1174
  }
1175

1176
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
57,065✔
1177
}
57,065✔
1178

1179
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
57,065✔
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
57,065✔
1181
  if (pTaskInfo == NULL) {
57,065✔
1182
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1183
  }
1184

1185
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
57,065✔
1186

1187
  setTaskKilled(pTaskInfo, rspCode);
57,065✔
1188
  qStopTaskOperators(pTaskInfo);
57,065✔
1189

1190
  return TSDB_CODE_SUCCESS;
57,065✔
1191
}
1192

1193
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1194
  int64_t        st = taosGetTimestampMs();
×
1195
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1196
  if (pTaskInfo == NULL) {
×
1197
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1198
  }
1199

1200
  if (waitDuration > 0) {
×
1201
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1202
  } else {
1203
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1204
  }
1205

1206
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1207

1208
  if (waitDuration > 0) {
×
1209
    while (1) {
1210
      taosWLockLatch(&pTaskInfo->lock);
×
1211
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1212
        taosWUnLockLatch(&pTaskInfo->lock);
×
1213

1214
        taosMsleep(200);
×
1215

1216
        int64_t d = taosGetTimestampMs() - st;
×
1217
        if (d >= waitDuration && waitDuration >= 0) {
×
1218
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1219
          return TSDB_CODE_SUCCESS;
×
1220
        }
1221
      } else {  // not running now
1222
        pTaskInfo->code = rspCode;
×
1223
        taosWUnLockLatch(&pTaskInfo->lock);
×
1224
        return TSDB_CODE_SUCCESS;
×
1225
      }
1226
    }
1227
  }
1228

1229
  int64_t et = taosGetTimestampMs() - st;
×
1230
  if (et < waitDuration) {
×
1231
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1232
    return TSDB_CODE_SUCCESS;
×
1233
  }
1234
  return TSDB_CODE_SUCCESS;
×
1235
}
1236

1237
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1239
  if (NULL == pTaskInfo) {
×
1240
    return false;
×
1241
  }
1242

1243
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1244
}
1245

1246
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
270,355,500✔
1247
  STaskCostInfo* pSummary = &pTaskInfo->cost;
270,355,500✔
1248
  int64_t        idleTime = pSummary->start - pSummary->created;
270,365,561✔
1249

1250
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
270,321,860✔
1251
  if (pSummary->pRecoder != NULL) {
270,330,056✔
1252
    qDebug(
186,560,395✔
1253
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1254
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1255
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1256
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1257
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1258
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1259
  } else {
1260
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
83,758,050✔
1261
           pSummary->elapsedTime / 1000.0);
1262
  }
1263
}
270,318,945✔
1264

1265

1266
void qDestroyTask(qTaskInfo_t qTaskHandle) {
277,098,128✔
1267
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
277,098,128✔
1268
  if (pTaskInfo == NULL) {
277,098,128✔
1269
    return;
6,752,512✔
1270
  }
1271

1272
  if (pTaskInfo->pRoot != NULL) {
270,345,616✔
1273
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
270,362,020✔
1274
  } else {
1275
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1276
  }
1277

1278
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
270,366,447✔
1279
  doDestroyTask(pTaskInfo);
270,351,113✔
1280
}
1281

1282
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
1,516,592✔
1283
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,516,592✔
1284
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
1,516,592✔
1285
}
1286

1287
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
330,134✔
1288
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
330,134✔
1289
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
330,134✔
1290

1291
  while (1) {
324,088✔
1292
    uint16_t type = pOperator->operatorType;
654,222✔
1293
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
654,222✔
1294
      *scanner = pOperator->info;
330,134✔
1295
      break;
330,134✔
1296
    } else {
1297
      pOperator = pOperator->pDownstream[0];
324,088✔
1298
    }
1299
  }
1300
}
330,134✔
1301

1302
void* qExtractReaderFromTmqScanner(void* scanner) {
329,849✔
1303
  SStreamScanInfo* pInfo = scanner;
329,849✔
1304
  return (void*)pInfo->tqReader;
329,849✔
1305
}
1306

1307
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
759,908✔
1308
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
759,908✔
1309
  return pTaskInfo->streamInfo.schema;
759,908✔
1310
}
1311

1312
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
759,908✔
1313
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
759,908✔
1314
  return pTaskInfo->streamInfo.tbName;
759,908✔
1315
}
1316

1317
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
797,036✔
1318
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
797,036✔
1319
  return &pTaskInfo->streamInfo.btMetaRsp;
797,036✔
1320
}
1321

1322
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
23,222,778✔
1323
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,222,778✔
1324
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
23,222,778✔
1325
  return 0;
23,222,778✔
1326
}
1327

1328
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
755,362✔
1329
  memset(pCond, 0, sizeof(SQueryTableDataCond));
755,362✔
1330
  pCond->order = TSDB_ORDER_ASC;
755,362✔
1331
  pCond->numOfCols = pMtInfo->schema->nCols;
755,957✔
1332
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
756,566✔
1333
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
755,691✔
1334
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
757,518✔
1335
    taosMemoryFreeClear(pCond->colList);
×
1336
    taosMemoryFreeClear(pCond->pSlotList);
×
1337
    return terrno;
×
1338
  }
1339

1340
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
756,265✔
1341
  pCond->suid = pMtInfo->suid;
755,649✔
1342
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
756,300✔
1343
  pCond->startVersion = -1;
755,957✔
1344
  pCond->endVersion = sContext->snapVersion;
756,265✔
1345

1346
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,779,090✔
1347
    SColumnInfo* pColInfo = &pCond->colList[i];
4,023,133✔
1348
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,022,559✔
1349
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,024,694✔
1350
    if (pMtInfo->pExtSchemas != NULL) {
4,022,524✔
1351
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,720✔
1352
    }
1353
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,021,614✔
1354
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,024,043✔
1355

1356
    pCond->pSlotList[i] = i;
4,022,531✔
1357
  }
1358

1359
  return TSDB_CODE_SUCCESS;
757,518✔
1360
}
1361

1362
void qStreamSetOpen(qTaskInfo_t tinfo) {
22,406,211✔
1363
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,406,211✔
1364
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,406,211✔
1365
  pOperator->status = OP_NOT_OPENED;
22,408,664✔
1366
}
22,406,516✔
1367

1368
void qStreamSetParams(qTaskInfo_t tinfo, int8_t sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
22,453,300✔
1369
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,453,300✔
1370
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
22,453,300✔
1371
  pTaskInfo->streamInfo.minPollRows = minPollRows;
22,455,002✔
1372
  pTaskInfo->streamInfo.timeout = timeout;
22,445,638✔
1373
  pTaskInfo->streamInfo.enableReplay = enableReplay;
22,447,333✔
1374
}
22,449,735✔
1375

1376
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
23,393,250✔
1377
  int32_t        code = TSDB_CODE_SUCCESS;
23,393,250✔
1378
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,393,250✔
1379
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
23,393,250✔
1380

1381
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
23,395,840✔
1382
  const char*    id = GET_TASKID(pTaskInfo);
23,395,151✔
1383

1384
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
23,394,564✔
1385
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
22,104,001✔
1386
    if (pOperator == NULL || code != 0) {
22,102,891✔
1387
      return code;
13✔
1388
    }
1389

1390
    SStreamScanInfo* pInfo = pOperator->info;
22,103,549✔
1391
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
22,102,192✔
1392
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
22,101,863✔
1393
    walReaderVerifyOffset(pWalReader, pOffset);
22,104,576✔
1394
  }
1395
  // if pOffset equal to current offset, means continue consume
1396
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
23,395,894✔
1397
    return 0;
21,402,426✔
1398
  }
1399

1400
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,988,673✔
1401
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,216,036✔
1402
    if (pOperator == NULL || code != 0) {
1,215,737✔
1403
      return code;
×
1404
    }
1405

1406
    SStreamScanInfo* pInfo = pOperator->info;
1,215,737✔
1407
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,215,751✔
1408
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,214,463✔
1409
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,215,423✔
1410

1411
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,214,779✔
1412
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,004,871✔
1413
      pScanBaseInfo->dataReader = NULL;
1,004,184✔
1414

1415
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
1,004,871✔
1416
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
1,004,871✔
1417
      walReaderVerifyOffset(pWalReader, pOffset);
1,004,542✔
1418
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
1,004,566✔
1419
      if (code < 0) {
1,004,871✔
1420
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,036✔
1421
        return code;
11,036✔
1422
      }
1423
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
210,527✔
1424
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1425
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1426
      int64_t uid = pOffset->uid;
211,493✔
1427
      int64_t ts = pOffset->ts;
211,493✔
1428
      int32_t index = 0;
210,541✔
1429

1430
      // this value may be changed if new tables are created
1431
      taosRLockLatch(&pTaskInfo->lock);
210,541✔
1432
      int32_t numOfTables = 0;
211,493✔
1433
      code = tableListGetSize(pTableListInfo, &numOfTables);
211,493✔
1434
      if (code != TSDB_CODE_SUCCESS) {
210,527✔
1435
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1436
        taosRUnLockLatch(&pTaskInfo->lock);
×
1437
        return code;
×
1438
      }
1439

1440
      if (uid == 0) {
210,527✔
1441
        if (numOfTables != 0) {
204,750✔
1442
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
39,212✔
1443
          if (!tmp) {
39,212✔
1444
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1445
            taosRUnLockLatch(&pTaskInfo->lock);
×
1446
            return terrno;
×
1447
          }
1448
          if (tmp) uid = tmp->uid;
39,212✔
1449
          ts = INT64_MIN;
39,212✔
1450
          pScanInfo->currentTable = 0;
39,212✔
1451
        } else {
1452
          taosRUnLockLatch(&pTaskInfo->lock);
165,538✔
1453
          qError("no table in table list, %s", id);
165,521✔
1454
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
165,843✔
1455
        }
1456
      }
1457
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
44,989✔
1458

1459
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
45,650✔
1460
             pInfo->pTableScanOp->resultInfo.totalRows);
1461
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
45,650✔
1462

1463
      // start from current accessed position
1464
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1465
      // position, let's find it from the beginning.
1466
      index = tableListFind(pTableListInfo, uid, 0);
45,650✔
1467
      taosRUnLockLatch(&pTaskInfo->lock);
45,650✔
1468

1469
      if (index >= 0) {
45,650✔
1470
        pScanInfo->currentTable = index;
45,650✔
1471
      } else {
1472
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1473
               numOfTables, pScanInfo->currentTable, id);
1474
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1475
      }
1476

1477
      STableKeyInfo keyInfo = {.uid = uid};
45,650✔
1478
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
45,650✔
1479

1480
      // let's start from the next ts that returned to consumer.
1481
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
45,650✔
1482
        pScanBaseInfo->cond.twindows.skey = ts;
913✔
1483
      } else {
1484
        pScanBaseInfo->cond.twindows.skey = ts + 1;
44,737✔
1485
      }
1486
      pScanInfo->scanTimes = 0;
45,650✔
1487

1488
      if (pScanBaseInfo->dataReader == NULL) {
45,650✔
1489
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
78,744✔
1490
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1491
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
39,372✔
1492
        if (code != TSDB_CODE_SUCCESS) {
39,372✔
1493
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1494
          return code;
×
1495
        }
1496

1497
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
39,372✔
1498
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1499
      } else {
1500
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,278✔
1501
        if (code != TSDB_CODE_SUCCESS) {
6,278✔
1502
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1503
          return code;
×
1504
        }
1505

1506
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,278✔
1507
        if (code != TSDB_CODE_SUCCESS) {
5,970✔
1508
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1509
          return code;
×
1510
        }
1511
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
5,970✔
1512
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1513
      }
1514

1515
      // restore the key value
1516
      pScanBaseInfo->cond.twindows.skey = oldSkey;
45,342✔
1517
    } else {
1518
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1519
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1520
    }
1521

1522
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1523
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
772,637✔
1524
      SStreamRawScanInfo* pInfo = pOperator->info;
758,368✔
1525
      SSnapContext*       sContext = pInfo->sContext;
758,368✔
1526
      SOperatorInfo*      p = NULL;
758,102✔
1527

1528
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
758,368✔
1529
      if (code != 0) {
758,368✔
1530
        return code;
×
1531
      }
1532

1533
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
758,368✔
1534

1535
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
758,368✔
1536
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1537
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1538
      }
1539

1540
      SMetaTableInfo mtInfo = {0};
758,368✔
1541
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
758,368✔
1542
      if (code != 0) {
756,863✔
1543
        destroyMetaTableInfo(&mtInfo);
1544
        return code;
×
1545
      }
1546
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
756,863✔
1547
      pInfo->dataReader = NULL;
756,513✔
1548

1549
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
757,416✔
1550
      tableListClear(pTableListInfo);
757,108✔
1551

1552
      if (mtInfo.uid == 0) {
757,717✔
1553
        destroyMetaTableInfo(&mtInfo);
1554
        goto end;  // no data
850✔
1555
      }
1556

1557
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
756,867✔
1558
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
756,006✔
1559
      if (code != TSDB_CODE_SUCCESS) {
756,265✔
1560
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1561
        destroyMetaTableInfo(&mtInfo);
1562
        return code;
×
1563
      }
1564
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
756,265✔
1565
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,310✔
1566
      } else {
1567
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
754,381✔
1568
      }
1569

1570
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
755,383✔
1571
      if (code != TSDB_CODE_SUCCESS) {
757,518✔
1572
        destroyMetaTableInfo(&mtInfo);
1573
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1574
        return code;
×
1575
      }
1576

1577
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
757,518✔
1578
      if (!pList) {
757,518✔
1579
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1580
        destroyMetaTableInfo(&mtInfo);
1581
        return code;
×
1582
      }
1583
      int32_t size = 0;
757,518✔
1584
      code = tableListGetSize(pTableListInfo, &size);
757,518✔
1585
      if (code != TSDB_CODE_SUCCESS) {
757,518✔
1586
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1587
        destroyMetaTableInfo(&mtInfo);
1588
        return code;
×
1589
      }
1590

1591
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,515,036✔
1592
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
757,518✔
1593
      if (code != TSDB_CODE_SUCCESS) {
756,307✔
1594
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1595
        destroyMetaTableInfo(&mtInfo);
1596
        return code;
×
1597
      }
1598

1599
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
756,307✔
1600
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
757,217✔
1601
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1602
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
755,362✔
1603
      pTaskInfo->streamInfo.schema = mtInfo.schema;
755,362✔
1604
      taosMemoryFreeClear(mtInfo.pExtSchemas);
757,217✔
1605

1606
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
757,217✔
1607
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
14,269✔
1608
      SStreamRawScanInfo* pInfo = pOperator->info;
4,828✔
1609
      SSnapContext*       sContext = pInfo->sContext;
4,828✔
1610
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,828✔
1611
      if (code != 0) {
4,828✔
1612
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1613
        return code;
×
1614
      }
1615
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,828✔
1616
             id);
1617
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,441✔
1618
      SStreamRawScanInfo* pInfo = pOperator->info;
9,441✔
1619
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,441✔
1620
      pInfo->dataReader = NULL;
9,441✔
1621
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,441✔
1622
    }
1623
  }
1624

1625
end:
1,812,016✔
1626
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,811,164✔
1627
  return 0;
1,812,122✔
1628
}
1629

1630
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
220,191,835✔
1631
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
220,191,835✔
1632
  if (pMsg->info.ahandle == NULL) {
220,231,190✔
1633
    rpcFreeCont(pMsg->pCont);
×
1634
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
×
1635
    return;
×
1636
  }
1637

1638
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
220,147,481✔
1639
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1640

1641
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
220,165,758✔
1642

1643
  if (pMsg->contLen > 0) {
220,212,757✔
1644
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
220,158,192✔
1645
    if (buf.pData == NULL) {
220,127,189✔
1646
      pMsg->code = terrno;
×
1647
    } else {
1648
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
220,127,189✔
1649
    }
1650
  }
1651

1652
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
220,221,895✔
1653
  rpcFreeCont(pMsg->pCont);
220,261,423✔
1654
  destroySendMsgInfo(pSendInfo);
220,211,061✔
1655
}
1656

1657
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1658
  int32_t        code = TSDB_CODE_SUCCESS;
×
1659
  int32_t        lino = 0;
×
1660
  SExecTaskInfo* pTaskInfo = tinfo;
×
1661
  SArray*        plist = NULL;
×
1662

1663
  code = getTableListInfo(pTaskInfo, &plist);
×
1664
  if (code || plist == NULL) {
×
1665
    return NULL;
×
1666
  }
1667

1668
  // only extract table in the first elements
1669
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1670

1671
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1672
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1673

1674
  int32_t numOfTables = 0;
×
1675
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1676
  QUERY_CHECK_CODE(code, lino, _end);
×
1677

1678
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1679
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1680
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1681
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1682
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1683
  }
1684

1685
  taosArrayDestroy(plist);
×
1686

1687
_end:
×
1688
  if (code != TSDB_CODE_SUCCESS) {
×
1689
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1690
    T_LONG_JMP(pTaskInfo->env, code);
×
1691
  }
1692
  return pUidList;
×
1693
}
1694

1695
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,394,702✔
1696
  int32_t        code = TSDB_CODE_SUCCESS;
3,394,702✔
1697
  int32_t        lino = 0;
3,394,702✔
1698
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,394,702✔
1699

1700
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,396,346✔
1701
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1702
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1703

1704
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1705
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1706
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,394,162✔
1707
    STableScanInfo* pScanInfo = pOperator->info;
1,698,175✔
1708

1709
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,698,175✔
1710
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,698,175✔
1711
  } else {
1712
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,697,079✔
1713
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,698,719✔
1714
    }
1715
  }
1716

1717
_end:
×
1718
  if (code != TSDB_CODE_SUCCESS) {
3,396,898✔
1719
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1720
  }
1721
  return code;
3,394,706✔
1722
}
1723

1724
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,697,623✔
1725
  if (pList == NULL) {
1,697,623✔
1726
    return TSDB_CODE_INVALID_PARA;
×
1727
  }
1728

1729
  *pList = NULL;
1,697,623✔
1730
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,698,171✔
1731
  if (pArray == NULL) {
1,699,267✔
1732
    return terrno;
×
1733
  }
1734

1735
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,699,267✔
1736
  if (code == 0) {
1,698,175✔
1737
    *pList = pArray;
1,698,175✔
1738
  } else {
1739
    taosArrayDestroy(pArray);
×
1740
  }
1741
  return code;
1,697,079✔
1742
}
1743

1744
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1745
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1746
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1747
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1748
  }
1749
  return 0;
×
1750
}
1751

1752
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1753
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1754
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1755
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1756
  }
1757
  return 0;
×
1758
}
1759

1760
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1761
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1762

1763
  int32_t code = pTaskInfo->code;
×
1764
  pTaskInfo->code = 0;
×
1765
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1766
}
×
1767

1768
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1769
  int32_t code = 0;
×
1770
  return code;
×
1771
}
1772

1773
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1774
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1775
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1776
  return code;
×
1777
}
1778

1779
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
30,026,547✔
1780
  int32_t code = 0;
30,026,547✔
1781

1782
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
30,026,547✔
1783
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
30,020,937✔
1784

1785
  if (pOper->fpSet.resetStateFn) {
30,021,968✔
1786
    code = pOper->fpSet.resetStateFn(pOper);
30,022,629✔
1787
  }
1788
  pOper->status = OP_NOT_OPENED;
30,018,054✔
1789
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
51,829,974✔
1790
    code = clearStatesForOperator(pOper->pDownstream[i]);
21,800,779✔
1791
  }
1792
  return code;
30,030,645✔
1793
}
1794

1795
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,225,041✔
1796
  int32_t        code = 0;
8,225,041✔
1797
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,225,041✔
1798
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,225,041✔
1799
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,225,041✔
1800
  code = clearStatesForOperator(pOper);
8,225,041✔
1801
  return code;
8,225,041✔
1802
}
1803

1804
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
11,160,534✔
1805
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,160,534✔
1806
  int64_t        threadId = taosGetSelfPthreadId();
11,160,534✔
1807
  int64_t        curOwner = 0;
11,159,807✔
1808

1809
  *ppRes = NULL;
11,159,807✔
1810

1811
  // todo extract method
1812
  taosRLockLatch(&pTaskInfo->lock);
11,159,807✔
1813
  bool isKilled = isTaskKilled(pTaskInfo);
11,160,528✔
1814
  if (isKilled) {
11,160,528✔
1815
    // clearStreamBlock(pTaskInfo->pRoot);
1816
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1817

1818
    taosRUnLockLatch(&pTaskInfo->lock);
×
1819
    return pTaskInfo->code;
×
1820
  }
1821

1822
  if (pTaskInfo->owner != 0) {
11,160,528✔
1823
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1824
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1825

1826
    taosRUnLockLatch(&pTaskInfo->lock);
×
1827
    return pTaskInfo->code;
×
1828
  }
1829

1830
  pTaskInfo->owner = threadId;
11,160,750✔
1831
  taosRUnLockLatch(&pTaskInfo->lock);
11,160,750✔
1832

1833
  if (pTaskInfo->cost.start == 0) {
11,160,311✔
1834
    pTaskInfo->cost.start = taosGetTimestampUs();
317,759✔
1835
  }
1836

1837
  // error occurs, record the error code and return to client
1838
  int32_t ret = setjmp(pTaskInfo->env);
11,160,311✔
1839
  if (ret != TSDB_CODE_SUCCESS) {
13,419,507✔
1840
    pTaskInfo->code = ret;
2,260,611✔
1841
    (void)cleanUpUdfs();
2,260,611✔
1842
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,260,611✔
1843
    atomic_store_64(&pTaskInfo->owner, 0);
2,260,611✔
1844
    return pTaskInfo->code;
2,260,611✔
1845
  }
1846

1847
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
11,158,896✔
1848

1849
  int64_t st = taosGetTimestampUs();
11,160,750✔
1850

1851
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,160,750✔
1852
  if (code) {
8,899,917✔
1853
    pTaskInfo->code = code;
×
1854
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1855
  } else {
1856
    *finished = *ppRes == NULL;
8,899,917✔
1857
    code = blockDataCheck(*ppRes);
8,899,917✔
1858
  }
1859
  if (code) {
8,899,917✔
1860
    pTaskInfo->code = code;
×
1861
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1862
  }
1863

1864
  uint64_t el = (taosGetTimestampUs() - st);
8,900,139✔
1865

1866
  pTaskInfo->cost.elapsedTime += el;
8,900,139✔
1867
  if (NULL == *ppRes) {
8,900,139✔
1868
    *useconds = pTaskInfo->cost.elapsedTime;
5,501,431✔
1869
  }
1870

1871
  (void)cleanUpUdfs();
8,900,139✔
1872

1873
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,899,900✔
1874
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,900,139✔
1875

1876
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,900,139✔
1877
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1878

1879
  atomic_store_64(&pTaskInfo->owner, 0);
8,900,139✔
1880
  return pTaskInfo->code;
8,900,139✔
1881
}
1882

1883
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1884
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1885
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1886
// }
1887

1888
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
266,604✔
1889
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1890
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1891
  int32_t code = 0;                                        
266,604✔
1892
  if (*pTableListInfo != NULL) {
266,604✔
1893
    qDebug("table list already exists, no need to create again");
×
1894
    goto end;
×
1895
  }
1896
  STableListInfo* pList = tableListCreate();
266,822✔
1897
  if (pList == NULL) {
266,822✔
1898
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1899
    code = terrno;
×
1900
    goto end;
×
1901
  }
1902

1903
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
266,822✔
1904
  SReadHandle    pHandle = {.vnode = pVnode};
266,822✔
1905
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
266,822✔
1906

1907
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
266,822✔
1908
  if (code != 0) {
266,822✔
1909
    tableListDestroy(pList);
×
1910
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1911
    goto end;
×
1912
  }
1913
  *pTableListInfo = pList;
266,822✔
1914

1915
end:
266,822✔
1916
  return 0;
266,822✔
1917
}
1918

1919
static int32_t doFilterTableByTagCond(void* pVnode, STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
31,803✔
1920
  int32_t code = doFilterByTagCond(pListInfo->idInfo.suid, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, NULL);
31,803✔
1921
  if (code != 0) {
31,803✔
1922
    return code;
×
1923
  }
1924
  int32_t numOfTables = taosArrayGetSize(pUidList);
31,803✔
1925
  for (int i = 0; i < numOfTables; i++) {
60,201✔
1926
    void* tmp = taosArrayGet(pUidList, i);
28,398✔
1927
    if (tmp == NULL) {
28,398✔
1928
      return terrno;
×
1929
    }
1930
    STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
28,398✔
1931

1932
    void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
28,398✔
1933
    if (p == NULL) {
28,398✔
1934
      return terrno;
×
1935
    }
1936
  }
1937
  return code;
31,803✔
1938
}
1939

1940
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
31,803✔
1941
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1942
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1943
  int32_t code = TSDB_CODE_SUCCESS;
31,803✔
1944
  STableListInfo* pList = tableListCreate();
31,803✔
1945
  if (pList == NULL) {
31,803✔
1946
    code = terrno;
×
1947
    goto end;
×
1948
  }
1949
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
31,803✔
1950
  if (uidListCopy == NULL) {
31,803✔
1951
    code = terrno;
×
1952
    goto end;
×
1953
  }
1954
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
31,803✔
1955
  SReadHandle    pHandle = {.vnode = pVnode};
31,803✔
1956

1957
  pList->idInfo.suid = suid;
31,803✔
1958
  pList->idInfo.tableType = TD_SUPER_TABLE;
31,803✔
1959
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
31,803✔
1960
  if (code != TSDB_CODE_SUCCESS) {
31,803✔
1961
    goto end;
×
1962
  }                                              
1963
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
31,803✔
1964
  if (code != TSDB_CODE_SUCCESS) {
31,803✔
1965
    goto end;
×
1966
  }
1967
  *tableList = pList->pTableList;
31,803✔
1968
  pList->pTableList = NULL;
31,803✔
1969

1970
  taosArrayClear(uidList);
31,803✔
1971
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
63,606✔
1972
    void* tmp = taosArrayGet(uidListCopy, i);
31,803✔
1973
    if (tmp == NULL) {
31,803✔
1974
      continue;
×
1975
    }
1976
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
31,803✔
1977
    if (slot == NULL) {
31,803✔
1978
      if (taosArrayPush(uidList, tmp) == NULL) {
3,405✔
1979
        code = terrno;
×
1980
        goto end;
×
1981
      }
1982
    }
1983
  }
1984
end:
31,803✔
1985
  taosArrayDestroy(uidListCopy);
31,803✔
1986
  tableListDestroy(pList);
31,803✔
1987
  return code;
31,803✔
1988
}
1989

1990
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1991
//   int32_t index = -1;
1992
//   (void)taosThreadRwlockRdlock(lock);
1993
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1994
//     index = 0;
1995
//     goto end;
1996
//   }
1997
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1998
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1999

2000
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
2001
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
2002
//       index = i;
2003
//       goto end;
2004
//     }
2005
//   }
2006
// end:
2007
//   (void)taosThreadRwlockUnlock(lock);
2008
//   return index;
2009
// }
2010

2011
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
266,822✔
2012
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
266,822✔
2013
  STableListInfo* pList = pTableListInfo;
266,822✔
2014
  return pList->pTableList;
266,822✔
2015
}
2016

2017
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,362,118✔
2018

2019
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,737,070✔
2020
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,737,070✔
2021
  qDestroyTask(tInfo);
2,737,070✔
2022
}
2,737,070✔
2023

2024
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
746,478✔
2025
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
746,478✔
2026
}
2027

2028
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
802,873✔
2029
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
2030
  int32_t      code = 0;
802,873✔
2031
  SNode*       pNode = 0;
802,873✔
2032
  SNodeList*   pList = NULL;
802,873✔
2033
  SExprInfo*   pExprInfo = NULL;
802,873✔
2034
  int32_t      numOfExprs = 1;
802,873✔
2035
  int32_t*     offset = 0;
802,873✔
2036
  STargetNode* pTargetNode = NULL;
802,873✔
2037
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
802,873✔
2038
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
802,872✔
2039

2040
  if (code == 0) {
803,317✔
2041
    pTargetNode->dataBlockId = 0;
803,317✔
2042
    pTargetNode->pExpr = pNode;
803,317✔
2043
    pTargetNode->slotId = 0;
803,317✔
2044
  }
2045
  if (code == 0) {
803,317✔
2046
    code = nodesMakeList(&pList);
803,317✔
2047
  }
2048
  if (code == 0) {
803,317✔
2049
    code = nodesListAppend(pList, (SNode*)pTargetNode);
803,317✔
2050
  }
2051
  if (code == 0) {
803,317✔
2052
    pNode = NULL;
803,317✔
2053
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
803,317✔
2054
  }
2055

2056
  if (code == 0) {
803,317✔
2057
    const char* pVal = NULL;
803,317✔
2058
    int32_t     len = 0;
803,317✔
2059
    SNode*      pSclNode = NULL;
803,317✔
2060
    switch (pExprInfo->pExpr->nodeType) {
803,317✔
2061
      case QUERY_NODE_FUNCTION:
803,317✔
2062
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
803,317✔
2063
        break;
803,317✔
2064
      case QUERY_NODE_OPERATOR:
×
2065
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
2066
        break;
×
2067
      default:
×
2068
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2069
        break;
×
2070
    }
2071
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
803,317✔
2072
    SSDataBlock block = {0};
803,317✔
2073
    block.info.rows = 1;
803,317✔
2074
    SSDataBlock* pBlock = &block;
803,317✔
2075
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
803,317✔
2076
    if (tmp == NULL) {
803,317✔
2077
      code = terrno;
×
2078
    }
2079
    if (code == 0) {
803,317✔
2080
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
803,317✔
2081
      gTaskScalarExtra.pStreamRange = NULL;
803,317✔
2082
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
803,317✔
2083
    }
2084
    taosArrayDestroy(pBlockList);
803,095✔
2085
  }
2086
  nodesDestroyList(pList);
803,317✔
2087
  destroyExprInfo(pExprInfo, numOfExprs);
802,873✔
2088
  taosMemoryFreeClear(pExprInfo);
803,317✔
2089
  return code;
803,095✔
2090
}
2091

2092
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,096,599✔
2093
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,096,599✔
2094
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,096,599✔
2095
  int32_t        code = 0;
2,096,599✔
2096
  SNode*         pNode = NULL;
2,096,599✔
2097
  if (!pForceOutputCols) return 0;
2,096,599✔
2098
  if (!*pRes) {
56,172✔
2099
    code = createDataBlock(pRes);
56,172✔
2100
  }
2101

2102
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
56,172✔
2103
    int32_t idx = 0;
56,172✔
2104
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
224,495✔
2105
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
168,323✔
2106
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
168,323✔
2107
      colInfo.info.precision = pCol->type.precision;
168,323✔
2108
      colInfo.info.scale = pCol->type.scale;
168,323✔
2109
      code = blockDataAppendColInfo(*pRes, &colInfo);
168,323✔
2110
      if (code != 0) break;
168,323✔
2111
    }
2112
  }
2113

2114
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
56,172✔
2115
  if (code != TSDB_CODE_SUCCESS) {
56,172✔
2116
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2117
    return code;
×
2118
  }
2119

2120
  // loop all exprs for force output, execute all exprs
2121
  int32_t idx = 0;
56,172✔
2122
  int32_t rowIdx = (*pRes)->info.rows;
56,172✔
2123
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
56,172✔
2124
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
56,172✔
2125
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
224,495✔
2126
    SScalarParam   dst = {0};
168,323✔
2127
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
168,323✔
2128
    code = nodesStringToNode(pCol->expr, &pNode);
168,323✔
2129
    if (code != 0) break;
168,323✔
2130
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
168,323✔
2131
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
168,323✔
2132
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
112,151✔
2133
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
112,151✔
2134
    } else {
2135
      dst.columnData = pInfo;
56,172✔
2136
      dst.numOfRows = rowIdx;
56,172✔
2137
      dst.colAlloced = false;
56,172✔
2138
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
56,172✔
2139
    }
2140
    ++idx;
168,323✔
2141
    // TODO sclFreeParam(&dst);
2142
    nodesDestroyNode(pNode);
168,323✔
2143
    if (code != 0) break;
168,323✔
2144
  }
2145
  if (code == TSDB_CODE_SUCCESS) {
56,172✔
2146
    (*pRes)->info.rows++;
56,172✔
2147
  }
2148
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
56,172✔
2149
  return code;
56,172✔
2150
}
2151

2152
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
253,296✔
2153
  int32_t      code = 0;
253,296✔
2154
  const char*  pVal = NULL;
253,296✔
2155
  SScalarParam dst = {0};
253,296✔
2156
  int32_t      len = 0;
253,296✔
2157
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
253,296✔
2158
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
253,296✔
2159
  // execute the expr
2160
  switch (pExpr->type) {
253,296✔
2161
    case QUERY_NODE_VALUE: {
×
2162
      SValueNode* pValue = (SValueNode*)pExpr;
×
2163
      int32_t     type = pValue->node.resType.type;
×
2164
      if (!IS_STR_DATA_TYPE(type)) {
×
2165
        qError("invalid sub tb expr with non-str type");
×
2166
        code = TSDB_CODE_INVALID_PARA;
×
2167
        break;
×
2168
      }
2169
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2170
      if (pTmp == NULL) {
×
2171
        qError("invalid sub tb expr with null value");
×
2172
        code = TSDB_CODE_INVALID_PARA;
×
2173
        break;
×
2174
      }
2175
      pVal = varDataVal(pTmp);
×
2176
      len = varDataLen(pTmp);
×
2177
    } break;
×
2178
    case QUERY_NODE_FUNCTION: {
253,296✔
2179
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
253,296✔
2180
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
253,296✔
2181
        qError("invalid sub tb expr with non-str type func");
×
2182
        code = TSDB_CODE_INVALID_PARA;
×
2183
        break;
×
2184
      }
2185
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
253,296✔
2186
      if (!pCol) {
253,074✔
2187
        code = terrno;
×
2188
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2189
        break;
×
2190
      }
2191

2192
      pCol->hasNull = true;
253,074✔
2193
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
253,074✔
2194
      pCol->info.colId = 0;
253,074✔
2195
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
253,074✔
2196
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
253,074✔
2197
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
253,074✔
2198
      code = colInfoDataEnsureCapacity(pCol, 1, true);
253,296✔
2199
      if (code != 0) {
253,074✔
2200
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2201
        taosMemoryFree(pCol);
×
2202
        break;
×
2203
      }
2204
      dst.columnData = pCol;
253,074✔
2205
      dst.numOfRows = 1;
253,074✔
2206
      dst.colAlloced = true;
253,074✔
2207
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
253,074✔
2208
      if (colDataIsNull_var(dst.columnData, 0)) {
253,074✔
2209
        qInfo("invalid sub tb expr with null value");
2,803✔
2210
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,580✔
2211
      }
2212
      if (code == 0) {
252,851✔
2213
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
250,271✔
2214
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
250,494✔
2215
      }
2216
    } break;
253,073✔
2217
    default:
×
2218
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2219
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2220
      break;
×
2221
  }
2222
  if (code == 0) {
253,073✔
2223
    if (!pVal || len == 0) {
250,494✔
2224
      qError("tbname generated with no characters which is not allowed");
223✔
2225
      code = TSDB_CODE_INVALID_PARA;
×
2226
    }
2227
    if(len > TSDB_TABLE_NAME_LEN - 1) {
250,271✔
2228
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
446✔
2229
      len = TSDB_TABLE_NAME_LEN - 1;
446✔
2230
    }
2231

2232
    memcpy(tbname, pVal, len);
250,271✔
2233
    tbname[len] = '\0';  // ensure null terminated
250,271✔
2234
    if (NULL != strchr(tbname, '.')) {
250,716✔
2235
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2236
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2237
    }
2238
  }
2239
  // TODO free dst
2240
  sclFreeParam(&dst);
253,295✔
2241
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
253,296✔
2242
  return code;
253,296✔
2243
}
2244

2245
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
256,684✔
2246
  if (pParam) {
256,684✔
2247
    if (pParam->tbname) {
256,684✔
2248
      taosMemFree(pParam->tbname);
256,684✔
2249
      pParam->tbname = NULL;
256,684✔
2250
    }
2251
    if (pParam->stbname) {
256,684✔
2252
      taosMemFree(pParam->stbname);
256,684✔
2253
      pParam->stbname = NULL;
256,684✔
2254
    }
2255
    if (pParam->dbFName) {
256,684✔
2256
      taosMemFree(pParam->dbFName);
256,684✔
2257
      pParam->dbFName = NULL;
256,684✔
2258
    }
2259
    if (pParam->pFields) {
256,684✔
2260
      taosArrayDestroy(pParam->pFields);
256,684✔
2261
      pParam->pFields = NULL;
256,684✔
2262
    }
2263
    if (pParam->pTagFields) {
256,684✔
2264
      taosArrayDestroy(pParam->pTagFields);
152,750✔
2265
      pParam->pTagFields = NULL;
152,750✔
2266
    }
2267
    if (pParam->colCids) {
256,684✔
2268
      taosArrayDestroy(pParam->colCids);
1,787✔
2269
      pParam->colCids = NULL;
1,787✔
2270
    }
2271
    if (pParam->tagCids) {
256,684✔
2272
      taosArrayDestroy(pParam->tagCids);
1,148✔
2273
      pParam->tagCids = NULL;
1,148✔
2274
    }
2275
    taosMemFree(pParam);
256,684✔
2276
  }
2277
}
256,684✔
2278

2279
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
256,684✔
2280
  int32_t code = 0, lino = 0;
256,684✔
2281
  if (ppDst == NULL || pSrc == NULL) {
256,684✔
2282
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2283
  }
2284
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
256,684✔
2285
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
256,572✔
2286

2287
  (*ppDst)->suid = pSrc->suid;
256,684✔
2288
  (*ppDst)->sver = pSrc->sver;
256,572✔
2289
  (*ppDst)->tbType = pSrc->tbType;
256,684✔
2290
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
256,376✔
2291
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
256,572✔
2292

2293
  if (pSrc->stbname) {
256,572✔
2294
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
256,684✔
2295
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
256,684✔
2296
  }
2297

2298
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
256,572✔
2299
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
256,572✔
2300

2301
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
256,684✔
2302

2303
  if (pSrc->pFields && pSrc->pFields->size > 0) {
256,572✔
2304
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
256,376✔
2305
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
256,684✔
2306
  } else {
2307
    (*ppDst)->pFields = NULL;
×
2308
  }
2309
  
2310
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
256,376✔
2311
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
152,750✔
2312
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
152,750✔
2313
  } else {
2314
    (*ppDst)->pTagFields = NULL;
103,626✔
2315
  }
2316

2317
  if (pSrc->colCids && pSrc->colCids->size > 0) {
256,684✔
2318
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,787✔
2319
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,787✔
2320
  } else {
2321
    (*ppDst)->colCids = NULL;
254,589✔
2322
  }
2323

2324
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
256,684✔
2325
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,148✔
2326
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,148✔
2327
  } else {
2328
    (*ppDst)->tagCids = NULL;
255,536✔
2329
  }
2330

2331
_exit:
256,376✔
2332

2333
  if (code != 0) {
256,376✔
2334
    if (*ppDst) {
×
2335
      destroyStreamInserterParam(*ppDst);
×
2336
      *ppDst = NULL;
×
2337
    }
2338
    
2339
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2340
  }
2341
  return code;
256,464✔
2342
}
2343

2344
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
253✔
2345
  return doDropStreamTable(pMsgCb, pOutput, pReq);
253✔
2346
}
2347

2348
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2349
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2350
}
2351

2352
int32_t qFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
5,898✔
2353
  int32_t         code = TSDB_CODE_SUCCESS;
5,898✔
2354

2355
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
5,898✔
2356
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
5,898✔
2357
  if (code != TSDB_CODE_SUCCESS) {
5,898✔
2358
    goto end;
×
2359
  }
2360
end:
5,898✔
2361
  return code;
5,898✔
2362
}
2363

2364
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2365
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2366
    return true;
2,147,483,647✔
2367
  }
2368

2369
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2370
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2371
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2372
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2373
      return durationSatisfied;
2,147,483,647✔
2374
    case TRUE_FOR_COUNT_ONLY:
58,368✔
2375
      return countSatisfied;
58,368✔
2376
    case TRUE_FOR_AND:
58,368✔
2377
      return durationSatisfied && countSatisfied;
58,368✔
2378
    case TRUE_FOR_OR:
58,368✔
2379
      return durationSatisfied || countSatisfied;
58,368✔
2380
    default:
×
2381
      return true;
×
2382
  }
2383
}
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