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

taosdata / TDengine / #4973

03 Mar 2026 09:57AM UTC coverage: 67.69% (+0.05%) from 67.642%
#4973

push

travis-ci

web-flow
test(query): fix result of explain cases (#34658)

208339 of 307783 relevant lines covered (67.69%)

131201157.41 hits per line

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

69.82
/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) {
590,283,050✔
45
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
590,283,050✔
46
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
590,283,050✔
47
  gTaskScalarExtra.fp = qFetchRemoteNode;
590,385,755✔
48
}
590,206,119✔
49

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

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

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

71
static void initRefPool() {
550,172✔
72
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
550,172✔
73
  (void)atexit(cleanupRefPool);
550,172✔
74
}
550,172✔
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) {
51,409,986✔
158
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
51,409,986✔
159
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
51,411,853✔
160
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
23,564,394✔
161
    if (pStreamScanInfo->pTableScanOp != NULL) {
23,564,394✔
162
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
23,562,661✔
163
      if (pScanInfo->base.dataReader != NULL) {
23,564,020✔
164
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
266,641✔
165
        if (code) {
266,322✔
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);
27,846,703✔
173
  }
174

175
  return 0;
27,196,622✔
176
}
177

178
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
27,198,166✔
179
  SExecTaskInfo* pTaskInfo = tinfo;
27,198,166✔
180
  pTaskInfo->id.queryId = queryId;
27,198,166✔
181
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
27,198,520✔
182

183
  // set the idstr for tsdbReader
184
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
27,198,376✔
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) {
447,318✔
214
  if (msg == NULL) {  // create raw scan
447,318✔
215
    SExecTaskInfo* pTaskInfo = NULL;
121,735✔
216

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

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

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

233
  SSubplan* pPlan = NULL;
325,583✔
234
  int32_t   code = qStringToSubplan(msg, &pPlan);
329,380✔
235
  if (code != TSDB_CODE_SUCCESS) {
329,380✔
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;
329,380✔
242
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
329,380✔
243
  if (code != TSDB_CODE_SUCCESS) {
329,380✔
244
    qDestroyTask(pTaskInfo);
×
245
    terrno = code;
×
246
    return NULL;
×
247
  }
248

249
  return pTaskInfo;
329,380✔
250
}
251

252
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
687,666✔
253
  if (streamInserterParam == NULL) {
687,666✔
254
    return TSDB_CODE_SUCCESS;
434,256✔
255
  }
256

257
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
253,410✔
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) {
253,410✔
263
    stError("insertParam: invalid db/table name");
×
264
    return TSDB_CODE_INVALID_PARA;
×
265
  }
266

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

273
  return TSDB_CODE_SUCCESS;
253,410✔
274
}
275

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

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

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

311
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
253,168✔
312
    if (NULL == pInserterParam) {
253,168✔
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);
253,168✔
318
    TSDB_CHECK_CODE(code, lino, _error);
252,949✔
319
    
320
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
252,949✔
321
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
253,410✔
322

323
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
253,410✔
324
    if (code) {
253,410✔
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,
687,545✔
329
         tstrerror(code));
330

331
_error:
71,845✔
332

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

342
bool qNeedReset(qTaskInfo_t pInfo) {
2,945,401✔
343
  if (pInfo == NULL) {
2,945,401✔
344
    return false;
×
345
  }
346
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,945,401✔
347
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,945,401✔
348
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,945,401✔
349
    return false;
×
350
  }
351
  int32_t node = nodeType(pOperator->pPhyNode);
2,945,401✔
352
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,644,776✔
353
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
426,534✔
354
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,590,177✔
355
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
356
}
357

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

363
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,518,867✔
364
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,518,867✔
365
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,518,488✔
366
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,518,488✔
367
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,518,008✔
368
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,518,387✔
369
}
370

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

378
  void*           info = pOperator->info;
2,945,401✔
379
  STableScanBase* pScanBaseInfo = NULL;
2,945,401✔
380

381
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,945,401✔
382
    pScanBaseInfo = &((STableScanInfo*)info)->base;
300,625✔
383
    setReadHandle(handle, pScanBaseInfo);
300,625✔
384
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,644,776✔
385
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,218,242✔
386
    setReadHandle(handle, pScanBaseInfo);
2,218,242✔
387
  }
388

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

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

400
  *pTaskInfo = NULL;
687,666✔
401

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

417
  return code;
687,666✔
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,
388,581✔
427
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
428
  int32_t code = TSDB_CODE_SUCCESS;
388,581✔
429
  int32_t lino = 0;
388,581✔
430
  int8_t  locked = 0;
388,581✔
431
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
388,581✔
432

433
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
388,581✔
434

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

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

444
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
388,581✔
445

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

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

455
  locked = 1;
388,581✔
456
  for (int32_t i = 0; i < numOfUids; ++i) {
869,952✔
457
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
481,371✔
458
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
481,371✔
459

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

466
    tDecoderClear(&mr.coder);
481,371✔
467

468
    if (mr.me.type == TSDB_SUPER_TABLE) {
481,371✔
469
      continue;
×
470
    } else {
471
      if (type == TSDB_SUPER_TABLE) {
481,371✔
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) {
481,371✔
474
          continue;
×
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};
481,371✔
486
    if (pScanInfo->pTagCond != NULL) {
481,371✔
487
      // tb_uid_t id = mr.me.uid;
488
      item.check = 1;
344,936✔
489
    }
490
    if (taosArrayPush(tUid, &item) == NULL) {
481,371✔
491
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
492
    }
493
  }
494

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

498
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
869,952✔
499
    bool     qualified = false;
481,371✔
500
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
481,371✔
501
    if (t == NULL) {
481,371✔
502
      continue;
×
503
    }
504

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

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

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

522
  // handle multiple partition
523

524
_end:
388,581✔
525

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

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

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

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

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

557
  return code;
1,408✔
558
}
559

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

571
  bool   assignUid = false;
388,581✔
572
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
388,581✔
573
  char*  keyBuf = NULL;
388,581✔
574
  if (bufLen > 0) {
388,581✔
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;
388,581✔
584
  taosWLockLatch(lock);
388,581✔
585

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

596
    if (bufLen > 0) {
308,891✔
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);
308,891✔
612
    if (code != TSDB_CODE_SUCCESS) {
308,891✔
613
      taosMemoryFree(keyBuf);
×
614
      taosArrayDestroy(qa);
×
615
      taosWUnLockLatch(lock);
×
616
      return code;
×
617
    }
618
  }
619

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

625
  taosArrayDestroy(qa);
388,581✔
626
  return 0;
388,581✔
627
}
628

629
static void qUpdateTableTagCache(SStreamScanInfo* pScanInfo, const SArray* tableIdList, col_id_t cid, SStorageAPI* api) {
389,231✔
630
  STqReader*   tqReader = pScanInfo->tqReader;
389,231✔
631
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
871,252✔
632
    int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
482,021✔
633
    api->tqReaderFn.tqUpdateTableTagCache(pScanInfo->tqReader, pScanInfo->pPseudoExpr, pScanInfo->numOfPseudoExpr, *uid, cid);
482,021✔
634
  }
635
}
389,231✔
636

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

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

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

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

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

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

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

670
  SStreamScanInfo* pScanInfo = pInfo->info;
650✔
671
  for (int32_t i = 0; i < taosArrayGetSize(cids); ++i) {
1,300✔
672
    col_id_t* cid = (col_id_t*)taosArrayGet(cids, i);
650✔
673
    qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
650✔
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,123,495✔
700
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
701
                                    int32_t idx, bool* tbGet) {
702
  *tbGet = false;
456,123,495✔
703

704
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
456,229,020✔
705
    return TSDB_CODE_INVALID_PARA;
×
706
  }
707
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
456,409,280✔
708

709
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
456,409,280✔
710
    return TSDB_CODE_SUCCESS;
269,576,726✔
711
  }
712

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

719
  *sversion = pSchemaInfo->sw->version;
186,698,396✔
720
  *tversion = pSchemaInfo->tversion;
186,930,491✔
721
  *rversion = pSchemaInfo->rversion;
186,806,108✔
722
  if (pSchemaInfo->dbname) {
186,723,806✔
723
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
186,685,664✔
724
  } else {
725
    dbName[0] = 0;
×
726
  }
727
  if (pSchemaInfo->tablename) {
187,014,743✔
728
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
186,731,850✔
729
  } else {
730
    tableName[0] = 0;
95,103✔
731
  }
732

733
  *tbGet = true;
186,895,939✔
734

735
  return TSDB_CODE_SUCCESS;
186,879,478✔
736
}
737

738
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
269,553,943✔
739

740
void qDestroyOperatorParam(SOperatorParam* pParam) {
123,952✔
741
  if (NULL == pParam) {
123,952✔
742
    return;
×
743
  }
744
  freeOperatorParam(pParam, OP_GET_PARAM);
123,952✔
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) {
35,016,741✔
753
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
35,016,741✔
754
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
35,016,741✔
755
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
35,016,741✔
756
    qError("%s, %s operator type mismatch, task operator type:%d, "
120,048✔
757
           "new param operator type:%d", GET_TASKID(pTask), __func__,
758
           pTask->pRoot->operatorType,
759
           pNewParam->opType);
760
    qDestroyOperatorParam((SOperatorParam*)pParam);
120,048✔
761
    return;
120,048✔
762
  }
763
  TSWAP(pParam, pTask->pOpParam);
34,902,357✔
764
  ((SExecTaskInfo*)tinfo)->paramSet = false;
34,899,789✔
765
}
766

767
int32_t qExecutorInit(void) {
4,339,585✔
768
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,339,585✔
769
  return TSDB_CODE_SUCCESS;
4,342,454✔
770
}
771

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

783
  code = tsem_wait(pSem);
2,915,922✔
784
  if (code != TSDB_CODE_SUCCESS) {
2,907,082✔
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,907,082✔
791
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,907,082✔
792
    if (code != TSDB_CODE_SUCCESS) {
2,907,082✔
793
      pTask->code = code;
×
794
      return pTask->code;
×
795
    }
796
  }
797
  return TSDB_CODE_SUCCESS;
2,907,082✔
798
}
799

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

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

809
  readHandle->uid = 0;
272,067,029✔
810
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
272,186,850✔
811
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
271,235,204✔
812
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
1,477,311✔
813
    goto _error;
445,170✔
814
  }
815
    
816
  if (handle) {
269,959,577✔
817
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
270,636,657✔
818
    void*           pSinkManager = NULL;
270,793,025✔
819
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
269,761,825✔
820
    if (code != TSDB_CODE_SUCCESS) {
269,161,505✔
821
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
822
      goto _error;
×
823
    }
824

825
    void* pSinkParam = NULL;
269,161,505✔
826
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
269,368,430✔
827
    if (code != TSDB_CODE_SUCCESS) {
269,221,472✔
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;
269,221,472✔
834
    if (readHandle->localExec) {
269,773,124✔
835
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,376✔
836
      if (code != TSDB_CODE_SUCCESS) {
2,376✔
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,
269,067,288✔
846
                              (*pTask)->id.str, pSubplan->processOneBlock);
270,411,730✔
847
    if (code) {
269,038,712✔
848
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
582✔
849
    }
850
  }
851

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

855
_error:
55,131,099✔
856
  // if failed to add ref for all tables in this query, abort current query
857
  return code;
271,805,714✔
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,
321,285,218✔
866
                     bool processOneBlock) {
867
  int32_t        code = TSDB_CODE_SUCCESS;
321,285,218✔
868
  int32_t        lino = 0;
321,285,218✔
869
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
321,285,218✔
870
  int64_t        threadId = taosGetSelfPthreadId();
321,285,218✔
871

872
  if (pLocal) {
321,181,759✔
873
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
317,754,578✔
874
  }
875

876
  taosArrayClear(pResList);
320,843,123✔
877

878
  int64_t curOwner = 0;
321,029,227✔
879
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
321,029,227✔
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) {
321,025,936✔
886
    pTaskInfo->cost.start = taosGetTimestampUs();
265,508,593✔
887
  }
888

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

895
  // error occurs, record the error code and return to client
896
  int32_t ret = setjmp(pTaskInfo->env);
321,060,127✔
897
  if (ret != TSDB_CODE_SUCCESS) {
321,227,309✔
898
    pTaskInfo->code = ret;
584,215✔
899
    (void)cleanUpUdfs();
584,215✔
900

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

904
    return pTaskInfo->code;
584,215✔
905
  }
906

907
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
320,643,094✔
908

909
  int32_t      current = 0;
320,669,741✔
910
  SSDataBlock* pRes = NULL;
320,669,741✔
911
  int64_t      st = taosGetTimestampUs();
321,145,949✔
912

913
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
321,145,949✔
914
    pTaskInfo->paramSet = true;
34,890,977✔
915
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
34,892,573✔
916
  } else {
917
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
286,368,659✔
918
  }
919

920
  QUERY_CHECK_CODE(code, lino, _end);
320,672,288✔
921
  code = blockDataCheck(pRes);
320,672,288✔
922
  QUERY_CHECK_CODE(code, lino, _end);
320,684,149✔
923

924
  if (pRes == NULL) {
320,684,149✔
925
    st = taosGetTimestampUs();
61,884,732✔
926
  }
927

928
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
320,679,362✔
929
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
320,682,842✔
930
    rowsThreshold = 4096;
319,940,015✔
931
  }
932

933
  int32_t blockIndex = 0;
320,469,186✔
934
  while (pRes != NULL) {
1,025,054,271✔
935
    SSDataBlock* p = NULL;
751,410,178✔
936
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
751,180,038✔
937
      SSDataBlock* p1 = NULL;
614,736,241✔
938
      code = createOneDataBlock(pRes, true, &p1);
614,748,336✔
939
      QUERY_CHECK_CODE(code, lino, _end);
614,710,867✔
940

941
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
614,710,867✔
942
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
614,922,247✔
943
      p = p1;
614,922,247✔
944
    } else if (processOneBlock) {
136,898,627✔
945
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
17,539,644✔
946
      if (tmp) {
17,539,644✔
947
        blockDataDestroy(*tmp);
17,539,644✔
948
        *tmp = NULL;
17,539,644✔
949
      }
950
      SSDataBlock* p1 = NULL;
17,539,644✔
951
      code = createOneDataBlock(pRes, true, &p1);
17,539,644✔
952
      QUERY_CHECK_CODE(code, lino, _end);
17,539,644✔
953

954
      *tmp = p1;
17,539,644✔
955
      p = p1;
17,539,644✔
956
    } else {
957
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
119,358,983✔
958
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
119,360,456✔
959

960
      p = *(SSDataBlock**)tmp;
119,360,456✔
961
      code = copyDataBlock(p, pRes);
119,358,392✔
962
      QUERY_CHECK_CODE(code, lino, _end);
119,356,925✔
963
    }
964

965
    blockIndex += 1;
751,814,764✔
966

967
    current += p->info.rows;
751,814,764✔
968
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
751,790,713✔
969
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
970
    void* tmp = taosArrayPush(pResList, &p);
751,802,504✔
971
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
751,802,504✔
972

973
    if (current >= rowsThreshold || processOneBlock) {
751,802,504✔
974
      break;
975
    }
976

977
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
704,696,444✔
978
    QUERY_CHECK_CODE(code, lino, _end);
704,312,891✔
979
    code = blockDataCheck(pRes);
704,312,891✔
980
    QUERY_CHECK_CODE(code, lino, _end);
704,728,241✔
981
  }
982

983
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
320,749,623✔
984
    pTaskInfo->pSubplan->rowsThreshold -= current;
555,918✔
985
  }
986

987
  *hasMore = (pRes != NULL);
320,736,953✔
988
  uint64_t el = (taosGetTimestampUs() - st);
320,745,620✔
989

990
  pTaskInfo->cost.elapsedTime += el;
320,745,620✔
991
  if (NULL == pRes) {
320,683,623✔
992
    *useconds = pTaskInfo->cost.elapsedTime;
273,578,417✔
993
  }
994

995
_end:
320,760,825✔
996
  (void)cleanUpUdfs();
320,742,389✔
997

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

1002
  atomic_store_64(&pTaskInfo->owner, 0);
320,918,618✔
1003
  if (code) {
320,916,195✔
1004
    pTaskInfo->code = code;
×
1005
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1006
  }
1007

1008
  return pTaskInfo->code;
320,916,195✔
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) {
24,870,149✔
1026
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
24,870,149✔
1027
  int64_t        threadId = taosGetSelfPthreadId();
24,870,149✔
1028
  int64_t        curOwner = 0;
24,869,853✔
1029

1030
  *pRes = NULL;
24,869,853✔
1031

1032
  // todo extract method
1033
  taosRLockLatch(&pTaskInfo->lock);
24,869,853✔
1034
  bool isKilled = isTaskKilled(pTaskInfo);
24,870,149✔
1035
  if (isKilled) {
24,869,853✔
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) {
24,869,853✔
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;
24,869,853✔
1051
  taosRUnLockLatch(&pTaskInfo->lock);
24,869,853✔
1052

1053
  if (pTaskInfo->cost.start == 0) {
24,870,149✔
1054
    pTaskInfo->cost.start = taosGetTimestampUs();
232,102✔
1055
  }
1056

1057
  // error occurs, record the error code and return to client
1058
  int32_t ret = setjmp(pTaskInfo->env);
24,870,149✔
1059
  if (ret != TSDB_CODE_SUCCESS) {
24,869,599✔
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));
24,869,599✔
1068

1069
  int64_t st = taosGetTimestampUs();
24,870,149✔
1070
  int32_t code = TSDB_CODE_SUCCESS;
24,870,149✔
1071
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
24,870,149✔
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);
24,870,149✔
1076
  }
1077
  if (code) {
24,868,083✔
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);
24,868,083✔
1083
  if (code) {
24,870,149✔
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);
24,869,791✔
1089

1090
  pTaskInfo->cost.elapsedTime += el;
24,869,791✔
1091
  if (NULL == *pRes) {
24,870,149✔
1092
    *useconds = pTaskInfo->cost.elapsedTime;
11,917,730✔
1093
  }
1094

1095
  (void)cleanUpUdfs();
24,870,149✔
1096

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

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

1103
  atomic_store_64(&pTaskInfo->owner, 0);
24,870,445✔
1104
  return pTaskInfo->code;
24,870,149✔
1105
}
1106

1107
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
101,684,934✔
1108
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
101,684,934✔
1109
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
101,687,854✔
1110
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
101,690,086✔
1111

1112
  if (!tmp) {
101,684,934✔
1113
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1114
    return terrno;
×
1115
  }
1116
  return TSDB_CODE_SUCCESS;
101,684,934✔
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) {
46,238✔
1142
  if (pTaskInfo->subJobCtx.hasSubJobs) {
46,238✔
1143
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
10,920✔
1144
    if (pTaskInfo->subJobCtx.param) {
10,920✔
1145
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
5,720✔
1146
    }
1147
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
10,920✔
1148
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
10,920✔
1149
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
10,920✔
1150
  }
1151
  
1152
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
46,238✔
1153

1154
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
46,238✔
1155
  for (int32_t i = 0; i < num; ++i) {
47,375✔
1156
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
1,137✔
1157
    if (!pStop) {
1,137✔
1158
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1159
      continue;
×
1160
    }
1161
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
1,137✔
1162
    if (pExchangeInfo) {
1,137✔
1163
      int32_t code = tsem_post(&pExchangeInfo->ready);
1,137✔
1164
      if (code != TSDB_CODE_SUCCESS) {
1,137✔
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);
1,137✔
1168
      }
1169
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
1,137✔
1170
      if (code != TSDB_CODE_SUCCESS) {
1,137✔
1171
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1172
      }
1173
    }
1174
  }
1175

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

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

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

1187
  setTaskKilled(pTaskInfo, rspCode);
46,238✔
1188
  qStopTaskOperators(pTaskInfo);
46,238✔
1189

1190
  return TSDB_CODE_SUCCESS;
46,238✔
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) {
272,576,404✔
1247
  STaskCostInfo* pSummary = &pTaskInfo->cost;
272,576,404✔
1248
  int64_t        idleTime = pSummary->start - pSummary->created;
272,600,971✔
1249

1250
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
272,522,219✔
1251
  if (pSummary->pRecoder != NULL) {
272,526,329✔
1252
    qDebug(
186,144,605✔
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,
86,363,691✔
1261
           pSummary->elapsedTime / 1000.0);
1262
  }
1263
}
272,509,928✔
1264

1265

1266
void qDestroyTask(qTaskInfo_t qTaskHandle) {
279,320,540✔
1267
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
279,320,540✔
1268
  if (pTaskInfo == NULL) {
279,320,540✔
1269
    return;
6,756,522✔
1270
  }
1271

1272
  if (pTaskInfo->pRoot != NULL) {
272,564,018✔
1273
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
272,595,082✔
1274
  } else {
1275
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1276
  }
1277

1278
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
272,602,508✔
1279
  doDestroyTask(pTaskInfo);
272,572,166✔
1280
}
1281

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

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

1291
  while (1) {
323,002✔
1292
    uint16_t type = pOperator->operatorType;
652,382✔
1293
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
652,382✔
1294
      *scanner = pOperator->info;
329,380✔
1295
      break;
329,380✔
1296
    } else {
1297
      pOperator = pOperator->pDownstream[0];
323,002✔
1298
    }
1299
  }
1300
}
329,380✔
1301

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

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

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

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

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

1328
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
740,809✔
1329
  memset(pCond, 0, sizeof(SQueryTableDataCond));
740,809✔
1330
  pCond->order = TSDB_ORDER_ASC;
740,809✔
1331
  pCond->numOfCols = pMtInfo->schema->nCols;
741,099✔
1332
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
741,695✔
1333
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
740,805✔
1334
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
740,805✔
1335
    taosMemoryFreeClear(pCond->colList);
1,186✔
1336
    taosMemoryFreeClear(pCond->pSlotList);
×
1337
    return terrno;
×
1338
  }
1339

1340
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
739,025✔
1341
  pCond->suid = pMtInfo->suid;
740,213✔
1342
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
739,023✔
1343
  pCond->startVersion = -1;
740,509✔
1344
  pCond->endVersion = sContext->snapVersion;
740,211✔
1345

1346
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,682,042✔
1347
    SColumnInfo* pColInfo = &pCond->colList[i];
3,941,243✔
1348
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,940,347✔
1349
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,943,021✔
1350
    if (pMtInfo->pExtSchemas != NULL) {
3,940,647✔
1351
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,960✔
1352
    }
1353
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,940,641✔
1354
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,940,053✔
1355

1356
    pCond->pSlotList[i] = i;
3,940,947✔
1357
  }
1358

1359
  return TSDB_CODE_SUCCESS;
743,183✔
1360
}
1361

1362
void qStreamSetOpen(qTaskInfo_t tinfo) {
23,383,353✔
1363
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,383,353✔
1364
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
23,383,353✔
1365
  pOperator->status = OP_NOT_OPENED;
23,383,531✔
1366
}
23,379,543✔
1367

1368
void qStreamSetParams(qTaskInfo_t tinfo, int8_t sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
23,428,625✔
1369
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,428,625✔
1370
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
23,428,625✔
1371
  pTaskInfo->streamInfo.minPollRows = minPollRows;
23,428,670✔
1372
  pTaskInfo->streamInfo.timeout = timeout;
23,412,483✔
1373
  pTaskInfo->streamInfo.enableReplay = enableReplay;
23,414,457✔
1374
}
23,418,491✔
1375

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

1381
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
24,358,904✔
1382
  const char*    id = GET_TASKID(pTaskInfo);
24,357,274✔
1383

1384
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
24,356,289✔
1385
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
23,080,836✔
1386
    if (pOperator == NULL || code != 0) {
23,079,494✔
1387
      return code;
×
1388
    }
1389

1390
    SStreamScanInfo* pInfo = pOperator->info;
23,080,898✔
1391
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
23,078,218✔
1392
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
23,075,168✔
1393
    walReaderVerifyOffset(pWalReader, pOffset);
23,079,146✔
1394
  }
1395
  // if pOffset equal to current offset, means continue consume
1396
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
24,355,356✔
1397
    return 0;
22,485,102✔
1398
  }
1399

1400
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,867,324✔
1401
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,108,811✔
1402
    if (pOperator == NULL || code != 0) {
1,110,066✔
1403
      return code;
320✔
1404
    }
1405

1406
    SStreamScanInfo* pInfo = pOperator->info;
1,109,746✔
1407
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,109,458✔
1408
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,109,113✔
1409
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,108,473✔
1410

1411
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,108,793✔
1412
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
895,276✔
1413
      pScanBaseInfo->dataReader = NULL;
894,582✔
1414

1415
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
894,582✔
1416
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
894,267✔
1417
      walReaderVerifyOffset(pWalReader, pOffset);
894,002✔
1418
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
894,921✔
1419
      if (code < 0) {
895,276✔
1420
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,044✔
1421
        return code;
11,044✔
1422
      }
1423
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
213,543✔
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;
214,790✔
1427
      int64_t ts = pOffset->ts;
214,470✔
1428
      int32_t index = 0;
213,830✔
1429

1430
      // this value may be changed if new tables are created
1431
      taosRLockLatch(&pTaskInfo->lock);
213,830✔
1432
      int32_t numOfTables = 0;
214,790✔
1433
      code = tableListGetSize(pTableListInfo, &numOfTables);
214,790✔
1434
      if (code != TSDB_CODE_SUCCESS) {
214,150✔
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) {
214,150✔
1441
        if (numOfTables != 0) {
207,715✔
1442
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,755✔
1443
          if (!tmp) {
38,755✔
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;
38,755✔
1449
          ts = INT64_MIN;
38,755✔
1450
          pScanInfo->currentTable = 0;
38,755✔
1451
        } else {
1452
          taosRUnLockLatch(&pTaskInfo->lock);
168,960✔
1453
          qError("no table in table list, %s", id);
169,248✔
1454
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
169,888✔
1455
        }
1456
      }
1457
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,190✔
1458

1459
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
44,587✔
1460
             pInfo->pTableScanOp->resultInfo.totalRows);
1461
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
44,902✔
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);
44,902✔
1467
      taosRUnLockLatch(&pTaskInfo->lock);
44,902✔
1468

1469
      if (index >= 0) {
44,902✔
1470
        pScanInfo->currentTable = index;
44,902✔
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};
44,902✔
1478
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
44,902✔
1479

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

1488
      if (pScanBaseInfo->dataReader == NULL) {
44,902✔
1489
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
78,482✔
1490
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1491
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
39,241✔
1492
        if (code != TSDB_CODE_SUCCESS) {
38,926✔
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",
38,926✔
1498
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1499
      } else {
1500
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
5,661✔
1501
        if (code != TSDB_CODE_SUCCESS) {
5,661✔
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);
5,661✔
1507
        if (code != TSDB_CODE_SUCCESS) {
5,661✔
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,661✔
1512
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1513
      }
1514

1515
      // restore the key value
1516
      pScanBaseInfo->cond.twindows.skey = oldSkey;
44,902✔
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) {
758,513✔
1524
      SStreamRawScanInfo* pInfo = pOperator->info;
744,333✔
1525
      SSnapContext*       sContext = pInfo->sContext;
744,333✔
1526
      SOperatorInfo*      p = NULL;
744,333✔
1527

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

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

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

1540
      SMetaTableInfo mtInfo = {0};
744,333✔
1541
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
744,333✔
1542
      if (code != 0) {
743,445✔
1543
        destroyMetaTableInfo(&mtInfo);
1544
        return code;
×
1545
      }
1546
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
743,445✔
1547
      pInfo->dataReader = NULL;
740,179✔
1548

1549
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
741,959✔
1550
      tableListClear(pTableListInfo);
740,471✔
1551

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

1557
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
743,185✔
1558
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
740,515✔
1559
      if (code != TSDB_CODE_SUCCESS) {
741,403✔
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)) {
741,403✔
1565
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,303✔
1566
      } else {
1567
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
739,506✔
1568
      }
1569

1570
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
740,805✔
1571
      if (code != TSDB_CODE_SUCCESS) {
743,481✔
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);
743,481✔
1578
      if (!pList) {
743,481✔
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;
743,481✔
1584
      code = tableListGetSize(pTableListInfo, &size);
743,481✔
1585
      if (code != TSDB_CODE_SUCCESS) {
743,481✔
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,486,962✔
1592
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
743,481✔
1593
      if (code != TSDB_CODE_SUCCESS) {
739,923✔
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);
739,923✔
1600
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
741,995✔
1601
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1602
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
739,035✔
1603
      pTaskInfo->streamInfo.schema = mtInfo.schema;
739,925✔
1604
      taosMemoryFreeClear(mtInfo.pExtSchemas);
742,593✔
1605

1606
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
742,593✔
1607
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
14,180✔
1608
      SStreamRawScanInfo* pInfo = pOperator->info;
4,816✔
1609
      SSnapContext*       sContext = pInfo->sContext;
4,816✔
1610
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,816✔
1611
      if (code != 0) {
4,816✔
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,816✔
1616
             id);
1617
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,364✔
1618
      SStreamRawScanInfo* pInfo = pOperator->info;
9,364✔
1619
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,364✔
1620
      pInfo->dataReader = NULL;
9,364✔
1621
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,364✔
1622
    }
1623
  }
1624

1625
end:
1,687,541✔
1626
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,686,622✔
1627
  return 0;
1,687,647✔
1628
}
1629

1630
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
222,809,705✔
1631
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
222,809,705✔
1632
  if (pMsg->info.ahandle == NULL) {
222,863,763✔
1633
    rpcFreeCont(pMsg->pCont);
580✔
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));
580✔
1635
    return;
580✔
1636
  }
1637

1638
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
222,726,007✔
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};
222,756,991✔
1642

1643
  if (pMsg->contLen > 0) {
222,814,108✔
1644
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
222,744,507✔
1645
    if (buf.pData == NULL) {
222,714,597✔
1646
      pMsg->code = terrno;
×
1647
    } else {
1648
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
222,714,597✔
1649
    }
1650
  }
1651

1652
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
222,863,962✔
1653
  rpcFreeCont(pMsg->pCont);
222,902,445✔
1654
  destroySendMsgInfo(pSendInfo);
222,790,708✔
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,418,615✔
1696
  int32_t        code = TSDB_CODE_SUCCESS;
3,418,615✔
1697
  int32_t        lino = 0;
3,418,615✔
1698
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,418,615✔
1699

1700
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,420,253✔
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,418,615✔
1707
    STableScanInfo* pScanInfo = pOperator->info;
1,709,306✔
1708

1709
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,709,306✔
1710
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,712,039✔
1711
  } else {
1712
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,710,401✔
1713
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,709,852✔
1714
    }
1715
  }
1716

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

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

1729
  *pList = NULL;
1,711,493✔
1730
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,711,493✔
1731
  if (pArray == NULL) {
1,710,944✔
1732
    return terrno;
×
1733
  }
1734

1735
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,710,944✔
1736
  if (code == 0) {
1,710,398✔
1737
    *pList = pArray;
1,710,398✔
1738
  } else {
1739
    taosArrayDestroy(pArray);
×
1740
  }
1741
  return code;
1,710,944✔
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) {
28,945,286✔
1780
  int32_t code = 0;
28,945,286✔
1781

1782
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
28,945,286✔
1783
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
28,942,653✔
1784

1785
  if (pOper->fpSet.resetStateFn) {
28,941,969✔
1786
    code = pOper->fpSet.resetStateFn(pOper);
28,942,542✔
1787
  }
1788
  pOper->status = OP_NOT_OPENED;
28,940,066✔
1789
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
49,774,134✔
1790
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,831,084✔
1791
  }
1792
  return code;
28,944,333✔
1793
}
1794

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

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

1809
  *ppRes = NULL;
11,432,247✔
1810

1811
  // todo extract method
1812
  taosRLockLatch(&pTaskInfo->lock);
11,432,247✔
1813
  bool isKilled = isTaskKilled(pTaskInfo);
11,432,470✔
1814
  if (isKilled) {
11,432,470✔
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,432,470✔
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,432,247✔
1831
  taosRUnLockLatch(&pTaskInfo->lock);
11,432,247✔
1832

1833
  if (pTaskInfo->cost.start == 0) {
11,431,993✔
1834
    pTaskInfo->cost.start = taosGetTimestampUs();
318,376✔
1835
  }
1836

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

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

1849
  int64_t st = taosGetTimestampUs();
11,432,247✔
1850

1851
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,432,247✔
1852
  if (code) {
9,391,058✔
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;
9,391,058✔
1857
    code = blockDataCheck(*ppRes);
9,391,765✔
1858
  }
1859
  if (code) {
9,391,792✔
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);
9,391,792✔
1865

1866
  pTaskInfo->cost.elapsedTime += el;
9,391,792✔
1867
  if (NULL == *ppRes) {
9,391,792✔
1868
    *useconds = pTaskInfo->cost.elapsedTime;
5,738,806✔
1869
  }
1870

1871
  (void)cleanUpUdfs();
9,391,792✔
1872

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

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

1879
  atomic_store_64(&pTaskInfo->owner, 0);
9,391,792✔
1880
  return pTaskInfo->code;
9,391,792✔
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,161✔
1889
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1890
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1891
  int32_t code = 0;                                        
266,161✔
1892
  if (*pTableListInfo != NULL) {
266,161✔
1893
    qDebug("table list already exists, no need to create again");
×
1894
    goto end;
×
1895
  }
1896
  STableListInfo* pList = tableListCreate();
266,161✔
1897
  if (pList == NULL) {
265,967✔
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};
265,967✔
1904
  SReadHandle    pHandle = {.vnode = pVnode};
266,040✔
1905
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
266,040✔
1906

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

1915
end:
266,161✔
1916
  return 0;
265,984✔
1917
}
1918

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

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

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

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

1970
  taosArrayClear(uidList);
30,151✔
1971
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
60,302✔
1972
    void* tmp = taosArrayGet(uidListCopy, i);
30,151✔
1973
    if (tmp == NULL) {
30,151✔
1974
      continue;
×
1975
    }
1976
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
30,151✔
1977
    if (slot == NULL) {
30,151✔
1978
      if (taosArrayPush(uidList, tmp) == NULL) {
3,420✔
1979
        code = terrno;
×
1980
        goto end;
×
1981
      }
1982
    }
1983
  }
1984
end:
30,151✔
1985
  taosArrayDestroy(uidListCopy);
30,151✔
1986
  tableListDestroy(pList);
30,151✔
1987
  return code;
30,151✔
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,161✔
2012
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
266,161✔
2013
  STableListInfo* pList = pTableListInfo;
266,161✔
2014
  return pList->pTableList;
266,161✔
2015
}
2016

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

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

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

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

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

2056
  if (code == 0) {
788,696✔
2057
    const char* pVal = NULL;
788,696✔
2058
    int32_t     len = 0;
788,696✔
2059
    SNode*      pSclNode = NULL;
788,696✔
2060
    switch (pExprInfo->pExpr->nodeType) {
788,696✔
2061
      case QUERY_NODE_FUNCTION:
788,696✔
2062
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
788,696✔
2063
        break;
788,696✔
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);
788,696✔
2072
    SSDataBlock block = {0};
788,817✔
2073
    block.info.rows = 1;
788,817✔
2074
    SSDataBlock* pBlock = &block;
788,817✔
2075
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
788,817✔
2076
    if (tmp == NULL) {
788,817✔
2077
      code = terrno;
×
2078
    }
2079
    if (code == 0) {
788,817✔
2080
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
788,817✔
2081
      gTaskScalarExtra.pStreamRange = NULL;
788,817✔
2082
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
788,817✔
2083
    }
2084
    taosArrayDestroy(pBlockList);
788,360✔
2085
  }
2086
  nodesDestroyList(pList);
788,594✔
2087
  destroyExprInfo(pExprInfo, numOfExprs);
788,817✔
2088
  taosMemoryFreeClear(pExprInfo);
788,583✔
2089
  return code;
788,487✔
2090
}
2091

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

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

2114
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
34,132✔
2115
  if (code != TSDB_CODE_SUCCESS) {
34,132✔
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;
34,132✔
2122
  int32_t rowIdx = (*pRes)->info.rows;
34,132✔
2123
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
34,132✔
2124
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
34,132✔
2125
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
136,332✔
2126
    SScalarParam   dst = {0};
102,200✔
2127
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
102,200✔
2128
    code = nodesStringToNode(pCol->expr, &pNode);
102,200✔
2129
    if (code != 0) break;
102,200✔
2130
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
102,200✔
2131
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
102,200✔
2132
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
68,068✔
2133
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
68,068✔
2134
    } else {
2135
      dst.columnData = pInfo;
34,132✔
2136
      dst.numOfRows = rowIdx;
34,132✔
2137
      dst.colAlloced = false;
34,132✔
2138
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
34,132✔
2139
    }
2140
    ++idx;
102,200✔
2141
    // TODO sclFreeParam(&dst);
2142
    nodesDestroyNode(pNode);
102,200✔
2143
    if (code != 0) break;
102,200✔
2144
  }
2145
  if (code == TSDB_CODE_SUCCESS) {
34,132✔
2146
    (*pRes)->info.rows++;
34,132✔
2147
  }
2148
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
34,132✔
2149
  return code;
34,132✔
2150
}
2151

2152
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
253,668✔
2153
  int32_t      code = 0;
253,668✔
2154
  const char*  pVal = NULL;
253,668✔
2155
  SScalarParam dst = {0};
253,668✔
2156
  int32_t      len = 0;
253,668✔
2157
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
253,668✔
2158
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
253,340✔
2159
  // execute the expr
2160
  switch (pExpr->type) {
253,668✔
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,668✔
2179
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
253,668✔
2180
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
253,668✔
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,668✔
2186
      if (!pCol) {
253,668✔
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,668✔
2193
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
253,668✔
2194
      pCol->info.colId = 0;
253,668✔
2195
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
253,340✔
2196
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
253,668✔
2197
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
253,668✔
2198
      code = colInfoDataEnsureCapacity(pCol, 1, true);
253,340✔
2199
      if (code != 0) {
253,668✔
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,668✔
2205
      dst.numOfRows = 1;
253,668✔
2206
      dst.colAlloced = true;
253,668✔
2207
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
253,668✔
2208
      if (colDataIsNull_var(dst.columnData, 0)) {
253,547✔
2209
        qInfo("invalid sub tb expr with null value");
3,802✔
2210
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,180✔
2211
      }
2212
      if (code == 0) {
253,046✔
2213
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
249,866✔
2214
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
249,718✔
2215
      }
2216
    } break;
253,459✔
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,459✔
2223
    if (!pVal || len == 0) {
249,838✔
2224
      qError("tbname generated with no characters which is not allowed");
×
2225
      code = TSDB_CODE_INVALID_PARA;
×
2226
    }
2227
    if(len > TSDB_TABLE_NAME_LEN - 1) {
250,072✔
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,072✔
2233
    tbname[len] = '\0';  // ensure null terminated
250,072✔
2234
    if (NULL != strchr(tbname, '.')) {
250,279✔
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,900✔
2241
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
253,225✔
2242
  return code;
253,225✔
2243
}
2244

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

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

2287
  (*ppDst)->suid = pSrc->suid;
253,168✔
2288
  (*ppDst)->sver = pSrc->sver;
253,184✔
2289
  (*ppDst)->tbType = pSrc->tbType;
252,942✔
2290
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
252,942✔
2291
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
253,410✔
2292

2293
  if (pSrc->stbname) {
253,184✔
2294
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
253,175✔
2295
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
253,410✔
2296
  }
2297

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

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

2303
  if (pSrc->pFields && pSrc->pFields->size > 0) {
253,184✔
2304
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
253,410✔
2305
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
253,175✔
2306
  } else {
2307
    (*ppDst)->pFields = NULL;
×
2308
  }
2309
  
2310
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
253,410✔
2311
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
152,831✔
2312
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
152,831✔
2313
  } else {
2314
    (*ppDst)->pTagFields = NULL;
100,344✔
2315
  }
2316

2317
  if (pSrc->colCids && pSrc->colCids->size > 0) {
253,175✔
2318
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,788✔
2319
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,788✔
2320
  } else {
2321
    (*ppDst)->colCids = NULL;
251,622✔
2322
  }
2323

2324
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
253,175✔
2325
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,146✔
2326
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,146✔
2327
  } else {
2328
    (*ppDst)->tagCids = NULL;
252,038✔
2329
  }
2330

2331
_exit:
252,949✔
2332

2333
  if (code != 0) {
252,949✔
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;
253,184✔
2342
}
2343

2344
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
254✔
2345
  return doDropStreamTable(pMsgCb, pOutput, pReq);
254✔
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) {
12,645✔
2353
  int32_t         code = TSDB_CODE_SUCCESS;
12,645✔
2354

2355
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
12,645✔
2356
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
12,645✔
2357
  if (code != TSDB_CODE_SUCCESS) {
12,645✔
2358
    goto end;
×
2359
  }
2360
end:
12,645✔
2361
  return code;
12,645✔
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:
57,856✔
2375
      return countSatisfied;
57,856✔
2376
    case TRUE_FOR_AND:
57,856✔
2377
      return durationSatisfied && countSatisfied;
57,856✔
2378
    case TRUE_FOR_OR:
57,856✔
2379
      return durationSatisfied || countSatisfied;
57,856✔
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