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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

70.88
/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             fetchObjRefPool = -1;
42
SGlobalExecInfo     gExecInfo = {0};
43

44
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
720,229,012✔
45
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
720,229,012✔
46
  gTaskScalarExtra.pSubJobCtx = pTaskInfo->pSubJobCtx;
720,229,012✔
47
  gTaskScalarExtra.fp = qFetchRemoteNode;
720,311,408✔
48
}
720,250,118✔
49

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

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

66
void doDestroyFetchObj(void* param) {
143,818,904✔
67
  if (param == NULL) {
143,818,904✔
68
    return;
×
69
  }
70

71
  if (*(bool*)param) {
143,818,904✔
72
    doDestroyExchangeOperatorInfo(param);
104,599,452✔
73
  } else {
74
    destroySubJobCtx((STaskSubJobCtx *)param);
39,221,412✔
75
  }
76
}
77

78
static void cleanupRefPool() {
593,555✔
79
  int32_t ref = atomic_val_compare_exchange_32(&fetchObjRefPool, fetchObjRefPool, 0);
593,555✔
80
  taosCloseRef(ref);
593,555✔
81
}
593,555✔
82

83
static void initRefPool() {
593,555✔
84
  fetchObjRefPool = taosOpenRef(1024, doDestroyFetchObj);
593,555✔
85
  (void)atexit(cleanupRefPool);
593,555✔
86
}
593,555✔
87

88
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
×
89
  int32_t code = TSDB_CODE_SUCCESS;
×
90
  int32_t lino = 0;
×
91
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
92
    if (pOperator->numOfDownstream == 0) {
×
93
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
94
      return TSDB_CODE_APP_ERROR;
×
95
    }
96

97
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
98
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
99
      return TSDB_CODE_APP_ERROR;
×
100
    }
101
    pOperator->status = OP_NOT_OPENED;
×
102
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
×
103
  } else {
104
    pOperator->status = OP_NOT_OPENED;
×
105
    return TSDB_CODE_SUCCESS;
×
106
  }
107

108
_end:
109
  if (code != TSDB_CODE_SUCCESS) {
110
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
111
  }
112
  return code;
113
}
114

115
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
116
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
117
    if (pOperator->numOfDownstream == 0) {
×
118
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
119
      return TSDB_CODE_APP_ERROR;
×
120
    }
121

122
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
123
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
124
      return TSDB_CODE_APP_ERROR;
×
125
    }
126

127
    pOperator->status = OP_NOT_OPENED;
×
128
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
129
  }
130
  return 0;
×
131
}
132

133
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
20,185,185✔
134
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
20,185,185✔
135
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
20,185,827✔
136
    STmqQueryScanInfo* pTmqScanInfo = pOperator->info;
7,838,992✔
137
    if (pTmqScanInfo->pTableScanOp != NULL) {
7,838,356✔
138
      STableScanInfo* pScanInfo = pTmqScanInfo->pTableScanOp->info;
7,839,292✔
139
      if (pScanInfo->base.dataReader != NULL) {
7,837,268✔
140
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
279,342✔
141
        if (code) {
279,365✔
142
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
143
          return code;
×
144
        }
145
      }
146
    }
147
  } else {
148
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
12,346,627✔
149
  }
150

151
  return 0;
11,677,841✔
152
}
153

154
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
11,677,276✔
155
  SExecTaskInfo* pTaskInfo = tinfo;
11,677,276✔
156
  pTaskInfo->id.queryId = queryId;
11,677,276✔
157
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
11,678,191✔
158

159
  // set the idstr for tsdbReader
160
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
11,678,866✔
161
}
162

163
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
164
  SExecTaskInfo* pTaskInfo = tinfo;
×
165
  return pTaskInfo->status == OP_EXEC_DONE;
×
166
}
167

168
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
169
  if (tinfo == NULL) {
×
170
    return TSDB_CODE_APP_ERROR;
×
171
  }
172

173
  if (pBlocks == NULL || numOfBlocks == 0) {
×
174
    return TSDB_CODE_SUCCESS;
×
175
  }
176

177
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
178

179
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
180
  if (code != TSDB_CODE_SUCCESS) {
×
181
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
182
  } else {
183
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
184
  }
185

186
  return code;
×
187
}
188

189
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
451,297✔
190
  if (msg == NULL) {  // create raw scan
451,297✔
191
    SExecTaskInfo* pTaskInfo = NULL;
115,794✔
192

193
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
115,794✔
194
    if (NULL == pTaskInfo || code != 0) {
115,794✔
195
      return NULL;
×
196
    }
197

198
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
115,794✔
199
    if (NULL == pTaskInfo->pRoot || code != 0) {
115,794✔
200
      taosMemoryFree(pTaskInfo);
×
201
      return NULL;
×
202
    }
203

204
    pTaskInfo->storageAPI = pReaderHandle->api;
115,794✔
205
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
115,794✔
206
    return pTaskInfo;
115,794✔
207
  }
208

209
  SSubplan* pPlan = NULL;
335,503✔
210
  int32_t   code = qStringToSubplan(msg, &pPlan);
337,669✔
211
  if (code != TSDB_CODE_SUCCESS) {
337,997✔
212
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
213
    terrno = code;
×
214
    return NULL;
×
215
  }
216

217
  qTaskInfo_t pTaskInfo = NULL;
337,997✔
218
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0,
337,997✔
219
                         NULL, OPTR_EXEC_MODEL_QUEUE, NULL, false);
220
  if (code != TSDB_CODE_SUCCESS) {
337,997✔
221
    qDestroyTask(pTaskInfo);
×
222
    terrno = code;
×
UNCOV
223
    return NULL;
×
224
  }
225

226
  return pTaskInfo;
337,997✔
227
}
228

229
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
725,072✔
230
  if (streamInserterParam == NULL) {
725,072✔
231
    return TSDB_CODE_SUCCESS;
461,543✔
232
  }
233

234
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
263,529✔
235
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
UNCOV
236
    return TSDB_CODE_INVALID_PARA;
×
237
  }
238

239
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
263,405✔
240
    stError("insertParam: invalid db/table name");
×
UNCOV
241
    return TSDB_CODE_INVALID_PARA;
×
242
  }
243

244
  if (streamInserterParam->suid <= 0 &&
263,529✔
245
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
103,512✔
246
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
UNCOV
247
    return TSDB_CODE_INVALID_PARA;
×
248
  }
249

250
  return TSDB_CODE_SUCCESS;
263,529✔
251
}
252

253
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
725,072✔
254
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
255
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
256
  if (pSubplan == NULL || pTaskInfo == NULL) {
725,072✔
257
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
124✔
258
    nodesDestroyNode((SNode *)pSubplan);
124✔
UNCOV
259
    return TSDB_CODE_INVALID_PARA;
×
260
  }
261
  int32_t lino = 0;
724,948✔
262
  int32_t code = checkInsertParam(streamInserterParam);
724,948✔
263
  if (code != TSDB_CODE_SUCCESS) {
725,072✔
264
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
265
    nodesDestroyNode((SNode *)pSubplan);
×
UNCOV
266
    return code;
×
267
  }
268
  SInserterParam* pInserterParam = NULL;
725,072✔
269
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
725,072✔
270
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
725,072✔
271
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
725,072✔
272

273
  int32_t subTaskNum = (int32_t)LIST_LENGTH(pSubplan->pSubQ);
725,072✔
274
  SArray* subEndPoints = taosArrayInit(subTaskNum, POINTER_BYTES);
725,072✔
275
  SDownstreamSourceNode* pSource = NULL;
725,072✔
276
  for (int32_t i = 0; i < subTaskNum; ++i) {
780,552✔
277
    SNode* pVal = nodesListGetNode(pSubplan->pSubQ, i);
55,728✔
278

279
    TSDB_CHECK_CODE(nodesCloneNode(pVal, (SNode**)&pSource), lino, _error);
55,728✔
280

281
    if (NULL == taosArrayPush(subEndPoints, &pSource)) {
111,456✔
282
      nodesDestroyNode((SNode *)pSource);
×
283
      code = terrno;
×
UNCOV
284
      goto _error;
×
285
    }
286
  }
287

288
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, &subEndPoints, false);
724,824✔
289
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
724,837✔
290
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
UNCOV
291
    goto _error;
×
292
  }
293

294
  if (streamInserterParam) {
725,072✔
295
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
263,529✔
296
    void*           pSinkManager = NULL;
263,529✔
297
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
263,529✔
298
    if (code != TSDB_CODE_SUCCESS) {
263,529✔
UNCOV
299
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
UNCOV
300
      goto _error;
×
301
    }
302

303
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
263,529✔
304
    if (NULL == pInserterParam) {
263,529✔
UNCOV
305
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
UNCOV
306
      code = terrno;
×
UNCOV
307
      goto _error;
×
308
    }
309
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
263,529✔
310
    TSDB_CHECK_CODE(code, lino, _error);
263,529✔
311
    
312
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
263,529✔
313
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
263,529✔
314

315
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
263,529✔
316
    if (code) {
263,059✔
UNCOV
317
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
318
    }
319
  }
320
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
724,948✔
321
         tstrerror(code));
322

323
_error:
724,250✔
324
  if (subEndPoints) {
725,072✔
325
    taosArrayDestroyP(subEndPoints, (FDelete)nodesDestroyNode);
709,580✔
326
  }
327

328
  if (code != TSDB_CODE_SUCCESS) {
725,072✔
UNCOV
329
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
UNCOV
330
    if (pInserterParam != NULL) {
×
UNCOV
331
      taosMemoryFree(pInserterParam);
×
332
    }
333
  }
334
  return code;
725,072✔
335
}
336

337
bool qNeedReset(qTaskInfo_t pInfo) {
3,115,437✔
338
  if (pInfo == NULL) {
3,115,437✔
UNCOV
339
    return false;
×
340
  }
341
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,115,437✔
342
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,115,437✔
343
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,115,437✔
UNCOV
344
    return false;
×
345
  }
346
  int32_t node = nodeType(pOperator->pPhyNode);
3,115,437✔
347
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,845,866✔
348
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
584,216✔
349
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,961,303✔
350
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
351
}
352

353
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
2,531,221✔
354
  if (pHandle == NULL || pScanBaseInfo == NULL) {
2,531,221✔
UNCOV
355
    return;
×
356
  }
357

358
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,531,221✔
359
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,531,221✔
360
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,531,221✔
361
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,531,221✔
362
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,531,221✔
363
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,531,221✔
364
}
365

366
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
3,115,437✔
367
  if (pInfo == NULL) {
3,115,437✔
UNCOV
368
    return TSDB_CODE_INVALID_PARA;
×
369
  }
370
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,115,437✔
371
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,115,437✔
372

373
  void*           info = pOperator->info;
3,115,437✔
374
  STableScanBase* pScanBaseInfo = NULL;
3,115,437✔
375

376
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,115,437✔
377
    pScanBaseInfo = &((STableScanInfo*)info)->base;
269,571✔
378
    setReadHandle(handle, pScanBaseInfo);
269,571✔
379
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,845,866✔
380
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,261,650✔
381
    setReadHandle(handle, pScanBaseInfo);
2,261,650✔
382
  }
383

384
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
3,115,437✔
385
  handle->winRange.ekey);
386
  return pOperator->fpSet.resetStateFn(pOperator);
3,115,437✔
387
}
388

389
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
724,837✔
390
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
391
  if (msg == NULL) {
724,837✔
UNCOV
392
    return TSDB_CODE_INVALID_PARA;
×
393
  }
394

395
  *pTaskInfo = NULL;
724,837✔
396

397
  SSubplan* pPlan = NULL;
725,072✔
398
  int32_t   code = qStringToSubplan(msg, &pPlan);
725,072✔
399
  if (code != TSDB_CODE_SUCCESS) {
725,072✔
UNCOV
400
    nodesDestroyNode((SNode *)pPlan);
×
UNCOV
401
    return code;
×
402
  }
403
  // todo: add stream inserter param
404
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
725,072✔
405
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
406
                               pInserterParams);
407
  if (code != TSDB_CODE_SUCCESS) {
725,072✔
UNCOV
408
    qDestroyTask(*pTaskInfo);
×
UNCOV
409
    return code;
×
410
  }
411

412
  return code;
725,072✔
413
}
414

415
typedef struct {
416
  tb_uid_t tableUid;
417
  tb_uid_t childUid;
418
  int8_t   check;
419
} STqPair;
420

421
static int32_t filterUnqualifiedTables(const STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
398,050✔
422
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
423
  int32_t code = TSDB_CODE_SUCCESS;
398,050✔
424
  int32_t lino = 0;
398,050✔
425
  int8_t  locked = 0;
398,050✔
426
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
398,050✔
427

428
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
398,050✔
429

430
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
398,050✔
431
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
398,050✔
432

433
  int32_t numOfUids = taosArrayGetSize(tableIdList);
398,050✔
434
  if (numOfUids == 0) {
398,050✔
UNCOV
435
    (*ppArrayRes) = qa;
×
UNCOV
436
    goto _error;
×
437
  }
438

439
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
398,050✔
440

441
  uint64_t suid = 0;
398,050✔
442
  uint64_t uid = 0;
398,050✔
443
  int32_t  type = 0;
398,050✔
444
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
398,050✔
445

446
  // let's discard the tables those are not created according to the queried super table.
447
  SMetaReader mr = {0};
398,050✔
448
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
398,050✔
449

450
  locked = 1;
398,050✔
451
  for (int32_t i = 0; i < numOfUids; ++i) {
838,490✔
452
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
440,440✔
453
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
440,440✔
454

455
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
440,440✔
456
    if (code != TSDB_CODE_SUCCESS) {
440,440✔
UNCOV
457
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
UNCOV
458
      continue;
×
459
    }
460

461
    tDecoderClear(&mr.coder);
440,440✔
462

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

480
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
440,440✔
481
    if (pScanInfo->pTagCond != NULL) {
440,440✔
482
      // tb_uid_t id = mr.me.uid;
483
      item.check = 1;
360,240✔
484
    }
485
    if (taosArrayPush(tUid, &item) == NULL) {
440,440✔
UNCOV
486
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
487
    }
488
  }
489

490
  pAPI->metaReaderFn.clearReader(&mr);
398,050✔
491
  locked = 0;
398,050✔
492

493
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
838,490✔
494
    bool     qualified = false;
440,440✔
495
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
440,440✔
496
    if (t == NULL) {
440,440✔
UNCOV
497
      continue;
×
498
    }
499

500
    if (t->check == 1) {
440,440✔
501
      code = isQualifiedTable(t->childUid, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
360,240✔
502
      if (code != TSDB_CODE_SUCCESS) {
360,240✔
UNCOV
503
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", t->childUid, idstr);
×
UNCOV
504
        continue;
×
505
      }
506

507
      if (!qualified) {
360,240✔
508
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
180,120✔
509
        continue;
180,120✔
510
      }
511
    }
512

513
    void* tmp = taosArrayPush(qa, &t->tableUid);
260,320✔
514
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
260,320✔
515
  }
516

517
  // handle multiple partition
518

519
_end:
398,050✔
520

521
  if (locked) {
398,050✔
UNCOV
522
    pAPI->metaReaderFn.clearReader(&mr);
×
523
  }
524
  (*ppArrayRes) = qa;
398,050✔
525
_error:
398,050✔
526

527
  taosArrayDestroy(tUid);
398,050✔
528
  if (code != TSDB_CODE_SUCCESS) {
398,050✔
UNCOV
529
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
530
  }
531
  return code;
398,050✔
532
}
533

534
int32_t qDeleteTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
1,496✔
535
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,496✔
536
  const char*    id = GET_TASKID(pTaskInfo);
1,496✔
537
  int32_t        code = 0;
1,496✔
538

539
  // traverse to the stream scanner node to add this table id
540
  SOperatorInfo* pInfo = NULL;
1,496✔
541
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
1,496✔
542
  if (code != 0 || pInfo == NULL) {
1,496✔
UNCOV
543
    return code;
×
544
  }
545

546
  STmqQueryScanInfo* pScanInfo = pInfo->info;
1,496✔
547
  qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
1,496✔
548
  taosWLockLatch(&pTaskInfo->lock);
1,496✔
549
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,496✔
550
  taosWUnLockLatch(&pTaskInfo->lock);
1,496✔
551

552
  return code;
1,496✔
553
}
554

555
static int32_t filterTableForTmqQuery(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* id, SStorageAPI* pAPI, SRWLatch* lock) {
398,050✔
556
  SArray* qa = NULL;
398,050✔
557
  int32_t code = filterUnqualifiedTables(pScanInfo, tableIdList, id, pAPI, &qa);
398,050✔
558
  if (code != TSDB_CODE_SUCCESS) {
398,050✔
UNCOV
559
    taosArrayDestroy(qa);
×
UNCOV
560
    return code;
×
561
  }
562
  int32_t numOfQualifiedTables = taosArrayGetSize(qa);
398,050✔
563
  qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
398,050✔
564
  pAPI->tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
398,050✔
565

566
  bool   assignUid = false;
398,050✔
567
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
398,050✔
568
  char*  keyBuf = NULL;
398,050✔
569
  if (bufLen > 0) {
398,050✔
UNCOV
570
    assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
UNCOV
571
    keyBuf = taosMemoryMalloc(bufLen);
×
UNCOV
572
    if (keyBuf == NULL) {
×
UNCOV
573
      taosArrayDestroy(qa);
×
574
      return terrno;
×
575
    }
576
  }
577

578
  STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
398,050✔
579
  taosWLockLatch(lock);
398,050✔
580

581
  for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
658,370✔
582
    uint64_t* uid = taosArrayGet(qa, i);
260,320✔
583
    if (!uid) {
260,320✔
UNCOV
584
      taosMemoryFree(keyBuf);
×
UNCOV
585
      taosArrayDestroy(qa);
×
UNCOV
586
      taosWUnLockLatch(lock);
×
UNCOV
587
      return terrno;
×
588
    }
589
    STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
260,320✔
590

591
    if (bufLen > 0) {
260,320✔
UNCOV
592
      if (assignUid) {
×
UNCOV
593
        keyInfo.groupId = keyInfo.uid;
×
594
      } else {
UNCOV
595
        code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
596
                                      &keyInfo.groupId, pAPI);
597
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
598
          taosMemoryFree(keyBuf);
×
599
          taosArrayDestroy(qa);
×
UNCOV
600
          taosWUnLockLatch(lock);
×
601
          return code;
×
602
        }
603
      }
604
    }
605

606
    code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
260,320✔
607
    if (code != TSDB_CODE_SUCCESS) {
260,320✔
UNCOV
608
      taosMemoryFree(keyBuf);
×
UNCOV
609
      taosArrayDestroy(qa);
×
UNCOV
610
      taosWUnLockLatch(lock);
×
UNCOV
611
      return code;
×
612
    }
613
  }
614

615
  taosWUnLockLatch(lock);
398,050✔
616
  if (keyBuf != NULL) {
398,050✔
UNCOV
617
    taosMemoryFree(keyBuf);
×
618
  }
619

620
  taosArrayDestroy(qa);
398,050✔
621
  return 0;
398,050✔
622
}
623

624
static void qUpdateTableTagCache(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, col_id_t cid, SStorageAPI* api) {
398,728✔
625
  STqReader*   tqReader = pScanInfo->tqReader;
398,728✔
626
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
839,846✔
627
    int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
441,118✔
628
    api->tqReaderFn.tqUpdateTableTagCache(pScanInfo->tqReader, pScanInfo->pPseudoExpr, pScanInfo->numOfPseudoExpr, *uid, cid);
441,118✔
629
  }
630
}
398,728✔
631

632
int32_t qAddTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
398,050✔
633
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
398,050✔
634
  const char*    id = GET_TASKID(pTaskInfo);
398,050✔
635
  int32_t        code = 0;
398,050✔
636

637
  qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
398,050✔
638

639
  // traverse to the stream scanner node to add this table id
640
  SOperatorInfo* pInfo = NULL;
398,050✔
641
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
398,050✔
642
  if (code != 0 || pInfo == NULL) {
398,050✔
UNCOV
643
    return code;
×
644
  }
645

646
  STmqQueryScanInfo* pScanInfo = pInfo->info;
398,050✔
647
  qUpdateTableTagCache(pScanInfo, tableIdList, 0, &pTaskInfo->storageAPI);
398,050✔
648

649
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
398,050✔
650
}
651

652
void qUpdateTableTagCacheForTmq(qTaskInfo_t tinfo, const SArray* tableIdList, SArray* cids) {
678✔
653
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
678✔
654
  const char*    id = GET_TASKID(pTaskInfo);
678✔
655
  int32_t        code = 0;
678✔
656

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

665
  STmqQueryScanInfo* pScanInfo = pInfo->info;
678✔
666
  for (int32_t i = 0; i < taosArrayGetSize(cids); ++i) {
1,356✔
667
    col_id_t* cid = (col_id_t*)taosArrayGet(cids, i);
678✔
668
    qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
678✔
669
  }
670
}
671

UNCOV
672
int32_t qUpdateTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
UNCOV
673
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
674
  const char*    id = GET_TASKID(pTaskInfo);
×
UNCOV
675
  int32_t        code = 0;
×
676

677
  // traverse to the stream scanner node to add this table id
678
  SOperatorInfo* pInfo = NULL;
×
679
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
×
UNCOV
680
  if (code != 0 || pInfo == NULL) {
×
UNCOV
681
    return code;
×
682
  }
683

684
  STmqQueryScanInfo* pScanInfo = pInfo->info;
×
685

UNCOV
686
  qDebug("%s %d remove child tables from the stream scanner, %s", __func__, (int32_t)taosArrayGetSize(tableIdList), id);
×
UNCOV
687
  taosWLockLatch(&pTaskInfo->lock);
×
688
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
UNCOV
689
  taosWUnLockLatch(&pTaskInfo->lock);
×
690
  
691
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
×
692
}
693

694
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
592,196,454✔
695
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
696
                                    int32_t idx, bool* tbGet) {
697
  *tbGet = false;
592,196,454✔
698

699
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
592,231,699✔
700
    return TSDB_CODE_INVALID_PARA;
240✔
701
  }
702
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
592,242,422✔
703

704
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
592,242,422✔
705
    return TSDB_CODE_SUCCESS;
336,243,912✔
706
  }
707

708
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
255,937,657✔
709
  if (!pSchemaInfo) {
255,838,916✔
UNCOV
710
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
711
    return terrno;
×
712
  }
713

714
  *sversion = pSchemaInfo->sw->version;
255,838,916✔
715
  *tversion = pSchemaInfo->tversion;
255,972,774✔
716
  *rversion = pSchemaInfo->rversion;
255,922,786✔
717
  if (pSchemaInfo->dbname) {
256,024,079✔
718
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
256,018,350✔
719
  } else {
UNCOV
720
    dbName[0] = 0;
×
721
  }
722
  if (pSchemaInfo->tablename) {
256,110,638✔
723
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
256,054,517✔
724
  } else {
UNCOV
725
    tableName[0] = 0;
×
726
  }
727

728
  *tbGet = true;
256,108,513✔
729

730
  return TSDB_CODE_SUCCESS;
256,041,843✔
731
}
732

733
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
336,201,376✔
734

735
void qDestroyOperatorParam(SOperatorParam* pParam) {
125,460✔
736
  if (NULL == pParam) {
125,460✔
UNCOV
737
    return;
×
738
  }
739
  freeOperatorParam(pParam, OP_GET_PARAM);
125,460✔
740
}
741

742
/**
743
  @brief Update the operator param for the task.
744
  @note  Unlike setOperatorParam, this function will destroy the new param when
745
         operator type mismatch.
746
*/
747
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
28,053,001✔
748
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
28,053,001✔
749
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
28,053,001✔
750
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
28,053,001✔
751
    qError("%s, %s operator type mismatch, task operator type:%d, "
120,540✔
752
           "new param operator type:%d", GET_TASKID(pTask), __func__,
753
           pTask->pRoot->operatorType,
754
           pNewParam->opType);
755
    qDestroyOperatorParam((SOperatorParam*)pParam);
120,540✔
756
    return;
120,540✔
757
  }
758
  TSWAP(pParam, pTask->pOpParam);
27,935,298✔
759
  ((SExecTaskInfo*)tinfo)->paramSet = false;
27,934,690✔
760
}
761

762
int32_t qExecutorInit(void) {
4,667,079✔
763
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,667,079✔
764
  return TSDB_CODE_SUCCESS;
4,667,805✔
765
}
766

767
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
36,741,679✔
768
  int32_t        code = TSDB_CODE_SUCCESS;
36,741,679✔
769
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
36,741,679✔
770
  if (pTask->pWorkerCb) {
36,741,679✔
771
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
36,743,823✔
772
    if (code != TSDB_CODE_SUCCESS) {
36,744,908✔
UNCOV
773
      pTask->code = code;
×
UNCOV
774
      return pTask->code;
×
775
    }
776
  }
777

778
  code = tsem_wait(pSem);
36,744,908✔
779
  if (code != TSDB_CODE_SUCCESS) {
36,744,372✔
UNCOV
780
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
781
    pTask->code = code;
×
UNCOV
782
    return pTask->code;
×
783
  }
784

785
  if (pTask->pWorkerCb) {
36,744,372✔
786
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
36,744,908✔
787
    if (code != TSDB_CODE_SUCCESS) {
36,745,444✔
UNCOV
788
      pTask->code = code;
×
UNCOV
789
      return pTask->code;
×
790
    }
791
  }
792
  return TSDB_CODE_SUCCESS;
36,744,908✔
793
}
794

795
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
338,777,666✔
796
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
797
                        EOPTR_EXEC_MODEL model, SArray** subEndPoints, bool enableExplain) {
798
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
338,777,666✔
799
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
338,777,666✔
800

801
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d, enableExplain:%d", 
338,785,824✔
802
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints ? *subEndPoints : NULL), enableExplain);
803

804
  readHandle->uid = 0;
338,821,290✔
805
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints, enableExplain);
338,849,528✔
806
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
338,637,423✔
807
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
644,584✔
808
    goto _error;
469,122✔
809
  }
810
    
811
  if (handle) {
338,036,194✔
812
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
337,856,323✔
813
    void*           pSinkManager = NULL;
337,910,730✔
814
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
337,540,293✔
815
    if (code != TSDB_CODE_SUCCESS) {
337,662,456✔
UNCOV
816
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
UNCOV
817
      goto _error;
×
818
    }
819

820
    void* pSinkParam = NULL;
337,662,456✔
821
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
337,814,689✔
822
    if (code != TSDB_CODE_SUCCESS) {
337,780,325✔
UNCOV
823
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
UNCOV
824
      taosMemoryFree(pSinkManager);
×
UNCOV
825
      goto _error;
×
826
    }
827

828
    SDataSinkNode* pSink = NULL;
337,780,325✔
829
    if (readHandle->localExec) {
337,819,807✔
830
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,772✔
831
      if (code != TSDB_CODE_SUCCESS) {
2,772✔
UNCOV
832
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
833
               (*pTask)->id.str);
UNCOV
834
        taosMemoryFree(pSinkManager);
×
UNCOV
835
        goto _error;
×
836
      }
837
    }
838

839
    // pSinkParam has been freed during create sinker.
840
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
337,623,744✔
841
                              (*pTask)->id.str, pSubplan->processOneBlock);
337,773,040✔
842
    if (code) {
337,634,908✔
843
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
610✔
844
    }
845
  }
846

847
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
338,092,000✔
848
    taskId, pSubplan->id.queryId, tstrerror(code), (*pTask)->pSubJobCtx ? (int32_t)taosArrayGetSize((*pTask)->pSubJobCtx->subEndPoints) : 0);
849

850
_error:
115,965,962✔
851
  // if failed to add ref for all tables in this query, abort current query
852
  return code;
338,766,420✔
853
}
854

UNCOV
855
static void freeBlock(void* param) {
×
UNCOV
856
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
UNCOV
857
  blockDataDestroy(pBlock);
×
UNCOV
858
}
×
859

860
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
384,438,061✔
861
                     bool processOneBlock) {
862
  int32_t        code = TSDB_CODE_SUCCESS;
384,438,061✔
863
  int32_t        lino = 0;
384,438,061✔
864
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
384,438,061✔
865
  int64_t        threadId = taosGetSelfPthreadId();
384,438,061✔
866

867
  if (pLocal) {
384,421,027✔
868
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
380,751,349✔
869
  }
870

871
  taosArrayClear(pResList);
384,317,964✔
872

873
  int64_t curOwner = 0;
384,374,988✔
874
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
384,374,988✔
UNCOV
875
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
876
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
UNCOV
877
    return pTaskInfo->code;
×
878
  }
879

880
  if (pTaskInfo->cost.start == 0) {
384,368,750✔
881
    pTaskInfo->cost.start = taosGetTimestampUs();
332,448,624✔
882
  }
883

884
  if (isTaskKilled(pTaskInfo)) {
384,407,781✔
UNCOV
885
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
886
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
887
    return pTaskInfo->code;
×
888
  }
889

890
  // error occurs, record the error code and return to client
891
  int32_t ret = setjmp(pTaskInfo->env);
384,369,182✔
892
  if (ret != TSDB_CODE_SUCCESS) {
386,476,985✔
893
    pTaskInfo->code = ret;
2,284,967✔
894
    (void)cleanUpUdfs();
2,284,967✔
895

896
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,284,967✔
897
    atomic_store_64(&pTaskInfo->owner, 0);
2,284,967✔
898

899
    return pTaskInfo->code;
2,284,967✔
900
  }
901

902
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
384,192,018✔
903

904
  int32_t      current = 0;
384,203,071✔
905
  SSDataBlock* pRes = NULL;
384,203,071✔
906
  int64_t      st = taosGetTimestampUs();
384,406,334✔
907

908
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
384,406,334✔
909
    pTaskInfo->paramSet = true;
27,931,386✔
910
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
27,933,216✔
911
  } else {
912
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
356,484,861✔
913
  }
914

915
  QUERY_CHECK_CODE(code, lino, _end);
382,169,280✔
916
  code = blockDataCheck(pRes);
382,169,280✔
917
  QUERY_CHECK_CODE(code, lino, _end);
382,185,774✔
918

919
  if (pRes == NULL) {
382,185,774✔
920
    st = taosGetTimestampUs();
77,252,215✔
921
  }
922

923
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
382,193,157✔
924
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
382,186,228✔
925
    rowsThreshold = 4096;
381,578,816✔
926
  }
927

928
  int32_t blockIndex = 0;
382,154,821✔
929
  while (pRes != NULL) {
1,092,089,050✔
930
    SSDataBlock* p = NULL;
755,400,255✔
931
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
755,282,433✔
932
      SSDataBlock* p1 = NULL;
619,075,498✔
933
      code = createOneDataBlock(pRes, true, &p1);
619,073,548✔
934
      QUERY_CHECK_CODE(code, lino, _end);
619,057,851✔
935

936
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
619,057,851✔
937
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
619,115,121✔
938
      p = p1;
619,115,121✔
939
    } else if (processOneBlock) {
136,387,342✔
940
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
14,602,244✔
941
      if (tmp) {
14,602,244✔
942
        blockDataDestroy(*tmp);
14,602,244✔
943
        *tmp = NULL;
14,602,244✔
944
      }
945
      SSDataBlock* p1 = NULL;
14,602,244✔
946
      code = createOneDataBlock(pRes, true, &p1);
14,602,244✔
947
      QUERY_CHECK_CODE(code, lino, _end);
14,602,244✔
948

949
      *tmp = p1;
14,602,244✔
950
      p = p1;
14,602,244✔
951
    } else {
952
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
121,785,098✔
953
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
121,783,379✔
954

955
      p = *(SSDataBlock**)tmp;
121,783,379✔
956
      code = copyDataBlock(p, pRes);
121,783,710✔
957
      QUERY_CHECK_CODE(code, lino, _end);
121,785,428✔
958
    }
959

960
    blockIndex += 1;
755,503,656✔
961

962
    current += p->info.rows;
755,503,656✔
963
    QUERY_CHECK_CONDITION((p->info.rows > 0), code, lino, _end,
755,501,681✔
964
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
965
    void* tmp = taosArrayPush(pResList, &p);
755,499,333✔
966
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
755,499,333✔
967

968
    if (current >= rowsThreshold || processOneBlock) {
755,499,333✔
969
      break;
970
    }
971

972
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
709,986,683✔
973
    QUERY_CHECK_CODE(code, lino, _end);
709,853,056✔
974
    code = blockDataCheck(pRes);
709,853,056✔
975
    QUERY_CHECK_CODE(code, lino, _end);
709,997,788✔
976
  }
977

978
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
382,201,445✔
979
    pTaskInfo->pSubplan->rowsThreshold -= current;
563,720✔
980
  }
981

982
  *hasMore = (pRes != NULL);
382,209,863✔
983
  uint64_t el = (taosGetTimestampUs() - st);
382,200,101✔
984

985
  pTaskInfo->cost.elapsedTime += el;
382,200,101✔
986
  if (NULL == pRes) {
382,204,917✔
987
    *useconds = pTaskInfo->cost.elapsedTime;
336,690,325✔
988
  }
989

990
_end:
382,280,431✔
991
  (void)cleanUpUdfs();
382,221,870✔
992

993
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
382,239,401✔
994
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
382,239,401✔
995
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
996

997
  atomic_store_64(&pTaskInfo->owner, 0);
382,241,186✔
998
  if (code) {
382,239,401✔
UNCOV
999
    pTaskInfo->code = code;
×
UNCOV
1000
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1001
  }
1002

1003
  return pTaskInfo->code;
382,239,401✔
1004
}
1005

UNCOV
1006
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
UNCOV
1007
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
1008
  SArray*        pList = pTaskInfo->pResultBlockList;
×
UNCOV
1009
  size_t         num = taosArrayGetSize(pList);
×
1010
  for (int32_t i = 0; i < num; ++i) {
×
1011
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
1012
    if (p) {
×
1013
      blockDataDestroy(*p);
×
1014
    }
1015
  }
1016

1017
  taosArrayClear(pTaskInfo->pResultBlockList);
×
UNCOV
1018
}
×
1019

1020
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
9,180,079✔
1021
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
9,180,079✔
1022
  int64_t        threadId = taosGetSelfPthreadId();
9,180,079✔
1023
  int64_t        curOwner = 0;
9,180,079✔
1024

1025
  *pRes = NULL;
9,180,079✔
1026

1027
  // todo extract method
1028
  taosRLockLatch(&pTaskInfo->lock);
9,180,079✔
1029
  bool isKilled = isTaskKilled(pTaskInfo);
9,180,079✔
1030
  if (isKilled) {
9,179,771✔
UNCOV
1031
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1032

UNCOV
1033
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1034
    return pTaskInfo->code;
×
1035
  }
1036

1037
  if (pTaskInfo->owner != 0) {
9,179,771✔
1038
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1039
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1040

UNCOV
1041
    taosRUnLockLatch(&pTaskInfo->lock);
×
1042
    return pTaskInfo->code;
×
1043
  }
1044

1045
  pTaskInfo->owner = threadId;
9,178,722✔
1046
  taosRUnLockLatch(&pTaskInfo->lock);
9,179,771✔
1047

1048
  if (pTaskInfo->cost.start == 0) {
9,180,079✔
1049
    pTaskInfo->cost.start = taosGetTimestampUs();
234,355✔
1050
  }
1051

1052
  // error occurs, record the error code and return to client
1053
  int32_t ret = setjmp(pTaskInfo->env);
9,179,771✔
1054
  if (ret != TSDB_CODE_SUCCESS) {
9,178,379✔
UNCOV
1055
    pTaskInfo->code = ret;
×
UNCOV
1056
    (void)cleanUpUdfs();
×
UNCOV
1057
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
UNCOV
1058
    atomic_store_64(&pTaskInfo->owner, 0);
×
1059
    return pTaskInfo->code;
×
1060
  }
1061

1062
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
9,178,379✔
1063

1064
  int64_t st = taosGetTimestampUs();
9,180,079✔
1065
  int32_t code = TSDB_CODE_SUCCESS;
9,180,079✔
1066
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
9,180,079✔
UNCOV
1067
    pTaskInfo->paramSet = true;
×
UNCOV
1068
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1069
  } else {
1070
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
9,180,079✔
1071
  }
1072
  if (code) {
9,179,736✔
UNCOV
1073
    pTaskInfo->code = code;
×
UNCOV
1074
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1075
  }
1076

1077
  code = blockDataCheck(*pRes);
9,179,736✔
1078
  if (code) {
9,179,787✔
UNCOV
1079
    pTaskInfo->code = code;
×
UNCOV
1080
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1081
  }
1082

1083
  uint64_t el = (taosGetTimestampUs() - st);
9,179,444✔
1084

1085
  pTaskInfo->cost.elapsedTime += el;
9,179,444✔
1086
  if (NULL == *pRes) {
9,179,736✔
1087
    *useconds = pTaskInfo->cost.elapsedTime;
5,714,239✔
1088
  }
1089

1090
  (void)cleanUpUdfs();
9,179,736✔
1091

1092
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
9,180,079✔
1093
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,180,079✔
1094

1095
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,180,079✔
1096
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1097

1098
  atomic_store_64(&pTaskInfo->owner, 0);
9,180,079✔
1099
  return pTaskInfo->code;
9,180,079✔
1100
}
1101

1102
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
104,599,847✔
1103
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
104,599,847✔
1104
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
104,599,847✔
1105
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
104,599,847✔
1106

1107
  if (!tmp) {
104,599,847✔
UNCOV
1108
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1109
    return terrno;
×
1110
  }
1111
  return TSDB_CODE_SUCCESS;
104,599,847✔
1112
}
1113

UNCOV
1114
int32_t stopInfoComp(void const* lp, void const* rp) {
×
UNCOV
1115
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
UNCOV
1116
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1117

1118
  if (key->refId < pInfo->refId) {
×
1119
    return -1;
×
1120
  } else if (key->refId > pInfo->refId) {
×
UNCOV
1121
    return 1;
×
1122
  }
1123

1124
  return 0;
×
1125
}
1126

UNCOV
1127
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1128
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
UNCOV
1129
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
UNCOV
1130
  if (idx >= 0) {
×
1131
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1132
  }
1133
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1134
}
×
1135

1136
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
73,821✔
1137
  if (pTaskInfo->pSubJobCtx) {
73,821✔
1138
    pTaskInfo->pSubJobCtx->code = pTaskInfo->code;
25,803✔
1139
    int32_t code = tsem_post(&pTaskInfo->pSubJobCtx->ready);
25,803✔
1140
  }
1141
  
1142
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
73,821✔
1143

1144
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
73,821✔
1145
  for (int32_t i = 0; i < num; ++i) {
78,173✔
1146
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
4,352✔
1147
    if (!pStop) {
4,352✔
UNCOV
1148
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1149
      continue;
×
1150
    }
1151
    SExchangeInfo* pExchangeInfo = taosAcquireRef(fetchObjRefPool, pStop->refId);
4,352✔
1152
    if (pExchangeInfo) {
4,352✔
1153
      int32_t code = tsem_post(&pExchangeInfo->ready);
4,352✔
1154
      if (code != TSDB_CODE_SUCCESS) {
4,352✔
UNCOV
1155
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1156
      } else {
1157
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
4,352✔
1158
      }
1159
      code = taosReleaseRef(fetchObjRefPool, pStop->refId);
4,352✔
1160
      if (code != TSDB_CODE_SUCCESS) {
4,352✔
UNCOV
1161
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1162
      }
1163
    }
1164
  }
1165

1166
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
73,821✔
1167
}
73,821✔
1168

1169
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
73,821✔
1170
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
73,821✔
1171
  if (pTaskInfo == NULL) {
73,821✔
UNCOV
1172
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1173
  }
1174

1175
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
73,821✔
1176

1177
  setTaskKilled(pTaskInfo, rspCode);
73,821✔
1178
  qStopTaskOperators(pTaskInfo);
73,821✔
1179

1180
  return TSDB_CODE_SUCCESS;
73,821✔
1181
}
1182

UNCOV
1183
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
UNCOV
1184
  int64_t        st = taosGetTimestampMs();
×
UNCOV
1185
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
1186
  if (pTaskInfo == NULL) {
×
1187
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1188
  }
1189

1190
  if (waitDuration > 0) {
×
1191
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1192
  } else {
UNCOV
1193
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1194
  }
1195

UNCOV
1196
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1197

UNCOV
1198
  if (waitDuration > 0) {
×
1199
    while (1) {
1200
      taosWLockLatch(&pTaskInfo->lock);
×
UNCOV
1201
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1202
        taosWUnLockLatch(&pTaskInfo->lock);
×
1203

1204
        taosMsleep(200);
×
1205

1206
        int64_t d = taosGetTimestampMs() - st;
×
UNCOV
1207
        if (d >= waitDuration && waitDuration >= 0) {
×
1208
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
UNCOV
1209
          return TSDB_CODE_SUCCESS;
×
1210
        }
1211
      } else {  // not running now
1212
        pTaskInfo->code = rspCode;
×
1213
        taosWUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1214
        return TSDB_CODE_SUCCESS;
×
1215
      }
1216
    }
1217
  }
1218

UNCOV
1219
  int64_t et = taosGetTimestampMs() - st;
×
UNCOV
1220
  if (et < waitDuration) {
×
UNCOV
1221
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
UNCOV
1222
    return TSDB_CODE_SUCCESS;
×
1223
  }
1224
  return TSDB_CODE_SUCCESS;
×
1225
}
1226

UNCOV
1227
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
UNCOV
1229
  if (NULL == pTaskInfo) {
×
UNCOV
1230
    return false;
×
1231
  }
1232

1233
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1234
}
1235

1236
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
339,212,467✔
1237
  STaskCostInfo* pSummary = &pTaskInfo->cost;
339,212,467✔
1238
  int64_t        idleTime = pSummary->start - pSummary->created;
339,225,646✔
1239

1240
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
339,213,034✔
1241
  if (pSummary->pRecoder != NULL) {
339,193,018✔
1242
    qDebug(
253,274,192✔
1243
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1244
        "createGroupIdMap:%.2f ms, total blocks:%" PRId64
1245
        ",load block SMA:%" PRId64 ", load data block:%" PRId64 ", check rows:%" PRId64,
1246
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0,
1247
        pSummary->extractListTime, pSummary->groupIdMapTime, pRecorder->totalBlocks,
1248
        pRecorder->smaLoadBlocks, pRecorder->fileLoadBlocks, pRecorder->checkRows);
1249
  } else {
1250
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
85,940,593✔
1251
           pSummary->elapsedTime / 1000.0);
1252
  }
1253
}
339,214,122✔
1254

1255

1256
void qDestroyTask(qTaskInfo_t qTaskHandle) {
346,312,877✔
1257
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
346,312,877✔
1258
  if (pTaskInfo == NULL) {
346,312,877✔
1259
    return;
7,104,489✔
1260
  }
1261

1262
  if (pTaskInfo->pRoot != NULL) {
339,208,388✔
1263
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
339,220,037✔
1264
  } else {
UNCOV
1265
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1266
  }
1267

1268
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
339,221,216✔
1269
  doDestroyTask(pTaskInfo);
339,210,725✔
1270
}
1271

1272
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
2,936,925✔
1273
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,936,925✔
1274
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
2,936,925✔
1275
}
1276

1277
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
337,997✔
1278
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
337,997✔
1279
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
337,997✔
1280

1281
  while (1) {
331,618✔
1282
    uint16_t type = pOperator->operatorType;
669,615✔
1283
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
669,615✔
1284
      *scanner = pOperator->info;
337,997✔
1285
      break;
337,997✔
1286
    } else {
1287
      pOperator = pOperator->pDownstream[0];
331,618✔
1288
    }
1289
  }
1290
}
337,997✔
1291

1292
void* qExtractReaderFromTmqScanner(void* scanner) {
337,997✔
1293
  STmqQueryScanInfo* pInfo = scanner;
337,997✔
1294
  return (void*)pInfo->tqReader;
337,997✔
1295
}
1296

1297
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
768,591✔
1298
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
768,591✔
1299
  return pTaskInfo->tmqInfo.schema;
768,591✔
1300
}
1301

1302
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
768,271✔
1303
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
768,271✔
1304
  return pTaskInfo->tmqInfo.tbName;
768,271✔
1305
}
1306

1307
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
843,996✔
1308
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
843,996✔
1309
  return &pTaskInfo->tmqInfo.btMetaRsp;
843,996✔
1310
}
1311

1312
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
8,455,840✔
1313
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
8,455,840✔
1314
  tOffsetCopy(pOffset, &pTaskInfo->tmqInfo.currentOffset);
8,455,840✔
1315
  return 0;
8,455,840✔
1316
}
1317

1318
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
808,028✔
1319
  memset(pCond, 0, sizeof(SQueryTableDataCond));
808,028✔
1320
  pCond->order = TSDB_ORDER_ASC;
808,028✔
1321
  pCond->numOfCols = pMtInfo->schema->nCols;
808,348✔
1322
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
808,028✔
1323
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
808,028✔
1324
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
808,348✔
UNCOV
1325
    taosMemoryFreeClear(pCond->colList);
×
UNCOV
1326
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1327
    return terrno;
×
1328
  }
1329

1330
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
808,348✔
1331
  pCond->suid = pMtInfo->suid;
808,348✔
1332
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
808,028✔
1333
  pCond->startVersion = -1;
808,668✔
1334
  pCond->endVersion = sContext->snapVersion;
808,668✔
1335

1336
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
5,183,027✔
1337
    SColumnInfo* pColInfo = &pCond->colList[i];
4,374,359✔
1338
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,374,039✔
1339
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,374,690✔
1340
    if (pMtInfo->pExtSchemas != NULL) {
4,374,999✔
1341
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
26,800✔
1342
    }
1343
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,375,010✔
1344
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,373,410✔
1345

1346
    pCond->pSlotList[i] = i;
4,374,370✔
1347
  }
1348

1349
  return TSDB_CODE_SUCCESS;
808,348✔
1350
}
1351

1352
void qStreamSetOpen(qTaskInfo_t tinfo) {
7,594,932✔
1353
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
7,594,932✔
1354
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
7,594,932✔
1355
  pOperator->status = OP_NOT_OPENED;
7,593,353✔
1356
}
7,594,955✔
1357

1358
void qStreamSetParams(qTaskInfo_t tinfo, int8_t sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
7,635,012✔
1359
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
7,635,012✔
1360
  pTaskInfo->tmqInfo.sourceExcluded = sourceExcluded;
7,635,012✔
1361
  pTaskInfo->tmqInfo.minPollRows = minPollRows;
7,637,355✔
1362
  pTaskInfo->tmqInfo.timeout = timeout;
7,623,583✔
1363
  pTaskInfo->tmqInfo.enableReplay = enableReplay;
7,624,553✔
1364
}
7,626,366✔
1365

1366
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
8,690,487✔
1367
  int32_t        code = TSDB_CODE_SUCCESS;
8,690,487✔
1368
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
8,690,487✔
1369
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
8,690,487✔
1370

1371
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
8,690,999✔
1372
  const char*    id = GET_TASKID(pTaskInfo);
8,689,638✔
1373

1374
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
8,685,236✔
1375
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
7,322,854✔
1376
    if (pOperator == NULL || code != 0) {
7,317,826✔
UNCOV
1377
      return code;
×
1378
    }
1379

1380
    STmqQueryScanInfo* pInfo = pOperator->info;
7,319,470✔
1381
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
7,321,179✔
1382
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
7,324,559✔
1383
    walReaderVerifyOffset(pWalReader, pOffset);
7,323,832✔
1384
  }
1385
  // if pOffset equal to current offset, means continue consume
1386
  if (tOffsetEqual(pOffset, &pTaskInfo->tmqInfo.currentOffset)) {
8,685,423✔
1387
    return 0;
7,371,584✔
1388
  }
1389

1390
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,312,696✔
1391
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
492,162✔
1392
    if (pOperator == NULL || code != 0) {
490,847✔
1393
      return code;
319✔
1394
    }
1395

1396
    STmqQueryScanInfo* pInfo = pOperator->info;
490,528✔
1397
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
491,843✔
1398
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
491,831✔
1399
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
491,176✔
1400

1401
    if (pOffset->type == TMQ_OFFSET__LOG) {
491,796✔
1402
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
291,216✔
1403
      pScanBaseInfo->dataReader = NULL;
290,480✔
1404

1405
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
290,480✔
1406
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
290,137✔
1407
      walReaderVerifyOffset(pWalReader, pOffset);
290,161✔
1408
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
291,216✔
1409
      if (code < 0) {
290,476✔
1410
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
87,976✔
1411
        return code;
88,716✔
1412
      }
1413
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
199,614✔
1414
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1415
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1416
      int64_t uid = pOffset->uid;
200,946✔
1417
      int64_t ts = pOffset->ts;
200,946✔
1418
      int32_t index = 0;
200,286✔
1419

1420
      // this value may be changed if new tables are created
1421
      taosRLockLatch(&pTaskInfo->lock);
200,286✔
1422
      int32_t numOfTables = 0;
200,946✔
1423
      code = tableListGetSize(pTableListInfo, &numOfTables);
200,946✔
1424
      if (code != TSDB_CODE_SUCCESS) {
200,274✔
UNCOV
1425
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1426
        taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1427
        return code;
×
1428
      }
1429

1430
      if (uid == 0) {
200,274✔
1431
        if (numOfTables != 0) {
193,477✔
1432
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
41,685✔
1433
          if (!tmp) {
41,685✔
UNCOV
1434
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1435
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1436
            return terrno;
×
1437
          }
1438
          if (tmp) uid = tmp->uid;
41,685✔
1439
          ts = INT64_MIN;
41,685✔
1440
          pScanInfo->currentTable = 0;
41,685✔
1441
        } else {
1442
          taosRUnLockLatch(&pTaskInfo->lock);
151,792✔
1443
          qError("no table in table list, %s", id);
152,800✔
1444
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
152,800✔
1445
        }
1446
      }
1447
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
48,482✔
1448

1449
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
47,486✔
1450
             pInfo->pTableScanOp->resultInfo.totalRows);
1451
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
47,815✔
1452

1453
      // start from current accessed position
1454
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1455
      // position, let's find it from the beginning.
1456
      index = tableListFind(pTableListInfo, uid, 0);
48,146✔
1457
      taosRUnLockLatch(&pTaskInfo->lock);
48,146✔
1458

1459
      if (index >= 0) {
48,146✔
1460
        pScanInfo->currentTable = index;
48,146✔
1461
      } else {
UNCOV
1462
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1463
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1464
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1465
      }
1466

1467
      STableKeyInfo keyInfo = {.uid = uid};
48,146✔
1468
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
48,146✔
1469

1470
      // let's start from the next ts that returned to consumer.
1471
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
48,146✔
1472
        pScanBaseInfo->cond.twindows.skey = ts;
972✔
1473
      } else {
1474
        pScanBaseInfo->cond.twindows.skey = ts + 1;
47,174✔
1475
      }
1476
      pScanInfo->scanTimes = 0;
48,146✔
1477

1478
      if (pScanBaseInfo->dataReader == NULL) {
48,146✔
1479
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
82,516✔
1480
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1481
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
41,258✔
1482
        if (code != TSDB_CODE_SUCCESS) {
41,258✔
UNCOV
1483
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
UNCOV
1484
          return code;
×
1485
        }
1486

1487
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
41,258✔
1488
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1489
      } else {
1490
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,888✔
1491
        if (code != TSDB_CODE_SUCCESS) {
6,888✔
UNCOV
1492
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1493
          return code;
×
1494
        }
1495

1496
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,888✔
1497
        if (code != TSDB_CODE_SUCCESS) {
6,888✔
UNCOV
1498
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1499
          return code;
×
1500
        }
1501
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
6,888✔
1502
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1503
      }
1504

1505
      // restore the key value
1506
      pScanBaseInfo->cond.twindows.skey = oldSkey;
48,146✔
1507
    } else {
UNCOV
1508
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
UNCOV
1509
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1510
    }
1511

1512
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1513
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
820,534✔
1514
      STmqRawScanInfo* pInfo = pOperator->info;
809,371✔
1515
      SSnapContext*       sContext = pInfo->sContext;
809,371✔
1516
      SOperatorInfo*      p = NULL;
809,371✔
1517

1518
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
809,371✔
1519
      if (code != 0) {
809,371✔
UNCOV
1520
        return code;
×
1521
      }
1522

1523
      STableListInfo* pTableListInfo = ((STmqRawScanInfo*)(p->info))->pTableListInfo;
809,371✔
1524

1525
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
809,371✔
UNCOV
1526
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
UNCOV
1527
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1528
      }
1529

1530
      SMetaTableInfo mtInfo = {0};
809,371✔
1531
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
809,371✔
1532
      if (code != 0) {
808,731✔
1533
        destroyMetaTableInfo(&mtInfo);
UNCOV
1534
        return code;
×
1535
      }
1536
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
808,731✔
1537
      pInfo->dataReader = NULL;
808,389✔
1538

1539
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
808,720✔
1540
      tableListClear(pTableListInfo);
808,720✔
1541

1542
      if (mtInfo.uid == 0) {
809,051✔
1543
        destroyMetaTableInfo(&mtInfo);
1544
        goto end;  // no data
703✔
1545
      }
1546

1547
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
808,348✔
1548
      code = initQueryTableDataCondForTmq(&pTaskInfo->tmqInfo.tableCond, sContext, &mtInfo);
808,028✔
1549
      if (code != TSDB_CODE_SUCCESS) {
807,708✔
UNCOV
1550
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1551
        destroyMetaTableInfo(&mtInfo);
UNCOV
1552
        return code;
×
1553
      }
1554
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
807,708✔
1555
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts;
1,387✔
1556
      } else {
1557
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts + 1;
806,641✔
1558
      }
1559

1560
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
808,028✔
1561
      if (code != TSDB_CODE_SUCCESS) {
808,668✔
1562
        destroyMetaTableInfo(&mtInfo);
UNCOV
1563
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1564
        return code;
×
1565
      }
1566

1567
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
808,668✔
1568
      if (!pList) {
808,668✔
UNCOV
1569
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1570
        destroyMetaTableInfo(&mtInfo);
UNCOV
1571
        return code;
×
1572
      }
1573
      int32_t size = 0;
808,668✔
1574
      code = tableListGetSize(pTableListInfo, &size);
808,668✔
1575
      if (code != TSDB_CODE_SUCCESS) {
808,668✔
UNCOV
1576
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1577
        destroyMetaTableInfo(&mtInfo);
UNCOV
1578
        return code;
×
1579
      }
1580

1581
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->tmqInfo.tableCond, pList, size,
1,617,336✔
1582
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
808,668✔
1583
      if (code != TSDB_CODE_SUCCESS) {
807,068✔
UNCOV
1584
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1585
        destroyMetaTableInfo(&mtInfo);
UNCOV
1586
        return code;
×
1587
      }
1588

1589
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
807,068✔
1590
      tstrncpy(pTaskInfo->tmqInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
807,708✔
1591
      //      pTaskInfo->tmqInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1592
      tDeleteSchemaWrapper(pTaskInfo->tmqInfo.schema);
808,668✔
1593
      pTaskInfo->tmqInfo.schema = mtInfo.schema;
807,388✔
1594
      taosMemoryFreeClear(mtInfo.pExtSchemas);
808,668✔
1595

1596
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
808,668✔
1597
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
11,163✔
1598
      STmqRawScanInfo* pInfo = pOperator->info;
2,462✔
1599
      SSnapContext*       sContext = pInfo->sContext;
2,462✔
1600
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
2,462✔
1601
      if (code != 0) {
2,462✔
UNCOV
1602
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
UNCOV
1603
        return code;
×
1604
      }
1605
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
2,462✔
1606
             id);
1607
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
8,701✔
1608
      STmqRawScanInfo* pInfo = pOperator->info;
8,701✔
1609
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
8,701✔
1610
      pInfo->dataReader = NULL;
8,701✔
1611
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
8,701✔
1612
    }
1613
  }
1614

1615
end:
1,071,070✔
1616
  tOffsetCopy(&pTaskInfo->tmqInfo.currentOffset, pOffset);
1,070,856✔
1617
  return 0;
1,071,180✔
1618
}
1619

1620
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
249,202,571✔
1621
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
249,202,571✔
1622
  if (pMsg->info.ahandle == NULL) {
249,235,471✔
1623
    rpcFreeCont(pMsg->pCont);
1,093✔
1624
    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));
1,093✔
1625
    return;
1,093✔
1626
  }
1627

1628
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
249,199,004✔
1629
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1630

1631
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
249,211,650✔
1632

1633
  if (pMsg->contLen > 0) {
249,218,849✔
1634
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
249,178,421✔
1635
    if (buf.pData == NULL) {
249,172,808✔
UNCOV
1636
      pMsg->code = terrno;
×
1637
    } else {
1638
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
249,172,808✔
1639
    }
1640
  }
1641

1642
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
249,237,796✔
1643
  rpcFreeCont(pMsg->pCont);
249,262,157✔
1644
  destroySendMsgInfo(pSendInfo);
249,228,700✔
1645
}
1646

UNCOV
1647
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
UNCOV
1648
  int32_t        code = TSDB_CODE_SUCCESS;
×
UNCOV
1649
  int32_t        lino = 0;
×
UNCOV
1650
  SExecTaskInfo* pTaskInfo = tinfo;
×
1651
  SArray*        plist = NULL;
×
1652

1653
  code = getTableListInfo(pTaskInfo, &plist);
×
1654
  if (code || plist == NULL) {
×
1655
    return NULL;
×
1656
  }
1657

1658
  // only extract table in the first elements
1659
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1660

UNCOV
1661
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
UNCOV
1662
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1663

UNCOV
1664
  int32_t numOfTables = 0;
×
1665
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1666
  QUERY_CHECK_CODE(code, lino, _end);
×
1667

1668
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1669
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1670
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
UNCOV
1671
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1672
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1673
  }
1674

1675
  taosArrayDestroy(plist);
×
1676

UNCOV
1677
_end:
×
UNCOV
1678
  if (code != TSDB_CODE_SUCCESS) {
×
1679
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
UNCOV
1680
    T_LONG_JMP(pTaskInfo->env, code);
×
1681
  }
1682
  return pUidList;
×
1683
}
1684

1685
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,545,662✔
1686
  int32_t        code = TSDB_CODE_SUCCESS;
3,545,662✔
1687
  int32_t        lino = 0;
3,545,662✔
1688
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,545,662✔
1689

1690
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,546,232✔
UNCOV
1691
    STmqQueryScanInfo* pScanInfo = pOperator->info;
×
UNCOV
1692
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1693

UNCOV
1694
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1695
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1696
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,543,955✔
1697
    STableScanInfo* pScanInfo = pOperator->info;
1,773,116✔
1698

1699
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,773,686✔
1700
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,774,256✔
1701
  } else {
1702
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,772,546✔
1703
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,773,113✔
1704
    }
1705
  }
1706

UNCOV
1707
_end:
×
1708
  if (code != TSDB_CODE_SUCCESS) {
3,545,092✔
UNCOV
1709
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1710
  }
1711
  return code;
3,545,089✔
1712
}
1713

1714
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,773,113✔
1715
  if (pList == NULL) {
1,773,113✔
UNCOV
1716
    return TSDB_CODE_INVALID_PARA;
×
1717
  }
1718

1719
  *pList = NULL;
1,773,113✔
1720
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,774,253✔
1721
  if (pArray == NULL) {
1,772,543✔
UNCOV
1722
    return terrno;
×
1723
  }
1724

1725
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,772,543✔
1726
  if (code == 0) {
1,771,976✔
1727
    *pList = pArray;
1,771,976✔
1728
  } else {
UNCOV
1729
    taosArrayDestroy(pArray);
×
1730
  }
1731
  return code;
1,770,836✔
1732
}
1733

UNCOV
1734
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
UNCOV
1735
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1736
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
UNCOV
1737
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1738
  }
1739
  return 0;
×
1740
}
1741

UNCOV
1742
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1743
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1744
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
UNCOV
1745
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1746
  }
1747
  return 0;
×
1748
}
1749

UNCOV
1750
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1751
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1752

UNCOV
1753
  int32_t code = pTaskInfo->code;
×
1754
  pTaskInfo->code = 0;
×
1755
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
UNCOV
1756
}
×
1757

1758
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1759
  int32_t code = 0;
×
1760
  return code;
×
1761
}
1762

1763
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1764
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1765
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
UNCOV
1766
  return code;
×
1767
}
1768

1769
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
29,886,737✔
1770
  int32_t code = 0;
29,886,737✔
1771

1772
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
29,886,737✔
1773
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
29,885,558✔
1774

1775
  if (pOper->fpSet.resetStateFn) {
29,886,226✔
1776
    code = pOper->fpSet.resetStateFn(pOper);
29,886,226✔
1777
  }
1778
  pOper->status = OP_NOT_OPENED;
29,880,607✔
1779
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
51,487,052✔
1780
    code = clearStatesForOperator(pOper->pDownstream[i]);
21,598,964✔
1781
  }
1782
  return code;
29,891,803✔
1783
}
1784

1785
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,287,749✔
1786
  int32_t        code = 0;
8,287,749✔
1787
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,287,749✔
1788
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,287,749✔
1789
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,287,749✔
1790
  code = clearStatesForOperator(pOper);
8,287,749✔
1791
  return code;
8,287,522✔
1792
}
1793

1794
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
11,277,632✔
1795
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,277,632✔
1796
  int64_t        threadId = taosGetSelfPthreadId();
11,277,632✔
1797
  int64_t        curOwner = 0;
11,277,397✔
1798

1799
  *ppRes = NULL;
11,277,397✔
1800

1801
  // todo extract method
1802
  taosRLockLatch(&pTaskInfo->lock);
11,277,632✔
1803
  bool isKilled = isTaskKilled(pTaskInfo);
11,277,632✔
1804
  if (isKilled) {
11,277,632✔
1805
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1806
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1807

UNCOV
1808
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1809
    return pTaskInfo->code;
×
1810
  }
1811

1812
  if (pTaskInfo->owner != 0) {
11,277,632✔
1813
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1814
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1815

UNCOV
1816
    taosRUnLockLatch(&pTaskInfo->lock);
×
1817
    return pTaskInfo->code;
×
1818
  }
1819

1820
  pTaskInfo->owner = threadId;
11,277,397✔
1821
  taosRUnLockLatch(&pTaskInfo->lock);
11,277,397✔
1822

1823
  if (pTaskInfo->cost.start == 0) {
11,277,632✔
1824
    pTaskInfo->cost.start = taosGetTimestampUs();
336,982✔
1825
  }
1826

1827
  // error occurs, record the error code and return to client
1828
  int32_t ret = setjmp(pTaskInfo->env);
11,277,632✔
1829
  if (ret != TSDB_CODE_SUCCESS) {
13,398,954✔
1830
    pTaskInfo->code = ret;
2,121,557✔
1831
    (void)cleanUpUdfs();
2,121,557✔
1832
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,121,557✔
1833
    atomic_store_64(&pTaskInfo->owner, 0);
2,121,557✔
1834
    return pTaskInfo->code;
2,121,557✔
1835
  }
1836

1837
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
11,277,397✔
1838

1839
  int64_t st = taosGetTimestampUs();
11,277,397✔
1840

1841
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,277,397✔
1842
  if (code) {
9,155,840✔
UNCOV
1843
    pTaskInfo->code = code;
×
UNCOV
1844
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1845
  } else {
1846
    *finished = *ppRes == NULL;
9,155,840✔
1847
    code = blockDataCheck(*ppRes);
9,156,075✔
1848
  }
1849
  if (code) {
9,156,075✔
UNCOV
1850
    pTaskInfo->code = code;
×
UNCOV
1851
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1852
  }
1853

1854
  uint64_t el = (taosGetTimestampUs() - st);
9,155,840✔
1855

1856
  pTaskInfo->cost.elapsedTime += el;
9,155,840✔
1857
  if (NULL == *ppRes) {
9,155,605✔
1858
    *useconds = pTaskInfo->cost.elapsedTime;
5,655,479✔
1859
  }
1860

1861
  (void)cleanUpUdfs();
9,155,840✔
1862

1863
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
9,156,075✔
1864
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,156,075✔
1865

1866
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,156,075✔
1867
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1868

1869
  atomic_store_64(&pTaskInfo->owner, 0);
9,156,075✔
1870
  return pTaskInfo->code;
9,156,075✔
1871
}
1872

1873
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1874
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1875
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1876
// }
1877

1878
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
271,996✔
1879
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1880
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1881
  int32_t code = 0;                                        
271,996✔
1882
  if (*pTableListInfo != NULL) {
271,996✔
UNCOV
1883
    qDebug("table list already exists, no need to create again");
×
UNCOV
1884
    goto end;
×
1885
  }
1886
  STableListInfo* pList = tableListCreate();
272,426✔
1887
  if (pList == NULL) {
272,426✔
1888
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1889
    code = terrno;
×
UNCOV
1890
    goto end;
×
1891
  }
1892

1893
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
272,426✔
1894
  SReadHandle    pHandle = {.vnode = pVnode};
272,426✔
1895
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
272,426✔
1896

1897
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
272,426✔
1898
  if (code != 0) {
272,426✔
UNCOV
1899
    tableListDestroy(pList);
×
UNCOV
1900
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
UNCOV
1901
    goto end;
×
1902
  }
1903
  *pTableListInfo = pList;
272,426✔
1904

1905
end:
272,426✔
1906
  return 0;
272,426✔
1907
}
1908

1909
static int32_t doFilterTableByTagCond(void* pVnode, STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
9,752✔
1910
  int32_t code = doFilterByTagCond(pListInfo->idInfo.suid, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, NULL);
9,752✔
1911
  if (code != 0) {
9,752✔
UNCOV
1912
    return code;
×
1913
  }
1914
  int32_t numOfTables = taosArrayGetSize(pUidList);
9,752✔
1915
  for (int i = 0; i < numOfTables; i++) {
19,504✔
1916
    void* tmp = taosArrayGet(pUidList, i);
9,752✔
1917
    if (tmp == NULL) {
9,752✔
UNCOV
1918
      return terrno;
×
1919
    }
1920
    STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
9,752✔
1921

1922
    void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
9,752✔
1923
    if (p == NULL) {
9,752✔
UNCOV
1924
      return terrno;
×
1925
    }
1926
  }
1927
  return code;
9,752✔
1928
}
1929

1930
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
9,752✔
1931
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1932
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1933
  int32_t code = TSDB_CODE_SUCCESS;
9,752✔
1934
  SArray* uidListCopy = NULL;
9,752✔
1935
  STableListInfo* pList = tableListCreate();
9,752✔
1936
  if (pList == NULL) {
9,752✔
UNCOV
1937
    code = terrno;
×
UNCOV
1938
    goto end;
×
1939
  }
1940
  uidListCopy = taosArrayDup(uidList, NULL);
9,752✔
1941
  if (uidListCopy == NULL) {
9,752✔
1942
    code = terrno;
×
UNCOV
1943
    goto end;
×
1944
  }
1945
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
9,752✔
1946
  SReadHandle    pHandle = {.vnode = pVnode};
9,752✔
1947

1948
  pList->idInfo.suid = suid;
9,752✔
1949
  pList->idInfo.tableType = TD_SUPER_TABLE;
9,752✔
1950
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
9,752✔
1951
  if (code != TSDB_CODE_SUCCESS) {
9,752✔
UNCOV
1952
    goto end;
×
1953
  }                                              
1954
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
9,752✔
1955
  if (code != TSDB_CODE_SUCCESS) {
9,752✔
1956
    goto end;
×
1957
  }
1958
  *tableList = pList->pTableList;
9,752✔
1959
  pList->pTableList = NULL;
9,752✔
1960

1961
  taosArrayClear(uidList);
9,752✔
1962
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
19,504✔
1963
    void* tmp = taosArrayGet(uidListCopy, i);
9,752✔
1964
    if (tmp == NULL) {
9,752✔
UNCOV
1965
      continue;
×
1966
    }
1967
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
9,752✔
1968
    if (slot == NULL) {
9,752✔
1969
      if (taosArrayPush(uidList, tmp) == NULL) {
×
UNCOV
1970
        code = terrno;
×
UNCOV
1971
        goto end;
×
1972
      }
1973
    }
1974
  }
1975
end:
9,752✔
1976
  taosArrayDestroy(uidListCopy);
9,752✔
1977
  tableListDestroy(pList);
9,752✔
1978
  return code;
9,752✔
1979
}
1980

1981
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1982
//   int32_t index = -1;
1983
//   (void)taosThreadRwlockRdlock(lock);
1984
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1985
//     index = 0;
1986
//     goto end;
1987
//   }
1988
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1989
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1990

1991
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1992
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1993
//       index = i;
1994
//       goto end;
1995
//     }
1996
//   }
1997
// end:
1998
//   (void)taosThreadRwlockUnlock(lock);
1999
//   return index;
2000
// }
2001

2002
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
272,426✔
2003
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
272,426✔
2004
  STableListInfo* pList = pTableListInfo;
272,426✔
2005
  return pList->pTableList;
272,426✔
2006
}
2007

2008
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,453,690✔
2009

2010
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,966,725✔
2011
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,966,725✔
2012
  qDestroyTask(tInfo);
2,966,725✔
2013
}
2,966,725✔
2014

2015
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
761,948✔
2016
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
761,948✔
2017
}
2018

2019
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
829,720✔
2020
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
2021
  int32_t      code = 0;
829,720✔
2022
  SNode*       pNode = NULL;
829,720✔
2023
  SNodeList*   pList = NULL;
829,720✔
2024
  SExprInfo*   pExprInfo = NULL;
829,962✔
2025
  int32_t      numOfExprs = 1;
829,962✔
2026
  int32_t*     offset = NULL;
829,962✔
2027
  STargetNode* pTargetNode = NULL;
829,962✔
2028
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
829,962✔
2029
  if (code == 0) {
829,720✔
2030
    code = nodesCloneNode(pExpr, &pNode);
829,720✔
2031
  }
2032

2033
  if (code == 0) {
829,962✔
2034
    pTargetNode->dataBlockId = 0;
829,962✔
2035
    pTargetNode->pExpr = pNode;
829,962✔
2036
    pTargetNode->slotId = 0;
829,962✔
2037
  }
2038
  if (code == 0) {
829,962✔
2039
    code = nodesMakeList(&pList);
829,962✔
2040
  }
2041
  if (code == 0) {
829,962✔
2042
    code = nodesListAppend(pList, (SNode*)pTargetNode);
829,962✔
2043
  }
2044
  if (code == 0) {
829,962✔
2045
    pNode = NULL;
829,962✔
2046
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
829,962✔
2047
  }
2048

2049
  if (code == 0) {
829,962✔
2050
    const char* pVal = NULL;
829,962✔
2051
    int32_t     len = 0;
829,962✔
2052
    SNode*      pSclNode = NULL;
829,962✔
2053
    switch (pExprInfo->pExpr->nodeType) {
829,962✔
2054
      case QUERY_NODE_FUNCTION:
829,962✔
2055
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
829,962✔
2056
        break;
829,779✔
UNCOV
2057
      case QUERY_NODE_OPERATOR:
×
UNCOV
2058
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
2059
        break;
×
UNCOV
2060
      default:
×
2061
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2062
        break;
×
2063
    }
2064
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
829,779✔
2065
    SSDataBlock block = {0};
829,962✔
2066
    block.info.rows = 1;
829,962✔
2067
    SSDataBlock* pBlock = &block;
829,962✔
2068
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
829,962✔
2069
    if (tmp == NULL) {
829,962✔
UNCOV
2070
      code = terrno;
×
2071
    }
2072
    if (code == 0) {
829,962✔
2073
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
829,727✔
2074
      gTaskScalarExtra.pStreamRange = NULL;
829,727✔
2075
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
829,962✔
2076
    }
2077
    taosArrayDestroy(pBlockList);
829,955✔
2078
  }
2079
  nodesDestroyList(pList);
829,727✔
2080
  destroyExprInfo(pExprInfo, numOfExprs);
829,354✔
2081
  taosMemoryFreeClear(pExprInfo);
829,051✔
2082
  return code;
829,217✔
2083
}
2084

2085
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,147,780✔
2086
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,147,780✔
2087
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,147,780✔
2088
  int32_t        code = 0;
2,147,780✔
2089
  SNode*         pNode = NULL;
2,147,780✔
2090
  if (!pForceOutputCols) return 0;
2,147,780✔
2091
  if (!*pRes) {
67,772✔
2092
    code = createDataBlock(pRes);
67,772✔
2093
  }
2094

2095
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
67,772✔
2096
    int32_t idx = 0;
67,772✔
2097
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
270,885✔
2098
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
203,113✔
2099
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
203,113✔
2100
      colInfo.info.precision = pCol->type.precision;
203,113✔
2101
      colInfo.info.scale = pCol->type.scale;
203,113✔
2102
      code = blockDataAppendColInfo(*pRes, &colInfo);
203,113✔
2103
      if (code != 0) break;
203,113✔
2104
    }
2105
  }
2106

2107
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
67,772✔
2108
  if (code != TSDB_CODE_SUCCESS) {
67,772✔
UNCOV
2109
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
2110
    return code;
×
2111
  }
2112

2113
  // loop all exprs for force output, execute all exprs
2114
  int32_t idx = 0;
67,772✔
2115
  int32_t rowIdx = (*pRes)->info.rows;
67,772✔
2116
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
67,772✔
2117
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
67,772✔
2118
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
270,885✔
2119
    SScalarParam   dst = {0};
203,113✔
2120
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
203,113✔
2121
    code = nodesStringToNode(pCol->expr, &pNode);
203,113✔
2122
    if (code != 0) break;
203,113✔
2123
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
203,113✔
2124
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
203,113✔
2125
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
135,341✔
2126
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
135,341✔
2127
    } else {
2128
      dst.columnData = pInfo;
67,772✔
2129
      dst.numOfRows = rowIdx;
67,772✔
2130
      dst.colAlloced = false;
67,772✔
2131
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
67,772✔
2132
    }
2133
    ++idx;
203,113✔
2134
    // TODO sclFreeParam(&dst);
2135
    nodesDestroyNode(pNode);
203,113✔
2136
    if (code != 0) break;
203,113✔
2137
  }
2138
  if (code == TSDB_CODE_SUCCESS) {
67,772✔
2139
    (*pRes)->info.rows++;
67,772✔
2140
  }
2141
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
67,772✔
2142
  return code;
67,772✔
2143
}
2144

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

2185
      pCol->hasNull = true;
257,239✔
2186
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
257,239✔
2187
      pCol->info.colId = 0;
257,004✔
2188
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
257,239✔
2189
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
257,239✔
2190
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
257,239✔
2191
      code = colInfoDataEnsureCapacity(pCol, 1, true);
257,239✔
2192
      if (code != 0) {
257,239✔
UNCOV
2193
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2194
        taosMemoryFree(pCol);
×
UNCOV
2195
        break;
×
2196
      }
2197
      dst.columnData = pCol;
257,239✔
2198
      dst.numOfRows = 1;
257,239✔
2199
      dst.colAlloced = true;
257,239✔
2200
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
257,239✔
2201
      if (colDataIsNull_var(dst.columnData, 0)) {
256,328✔
2202
        qInfo("invalid sub tb expr with null value");
4,094✔
2203
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,660✔
2204
      }
2205
      if (code == 0) {
256,439✔
2206
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
252,544✔
2207
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
252,407✔
2208
      }
2209
    } break;
256,570✔
UNCOV
2210
    default:
×
UNCOV
2211
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2212
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2213
      break;
×
2214
  }
2215
  if (code == 0) {
256,570✔
2216
    if (!pVal || len == 0) {
252,668✔
2217
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2218
      code = TSDB_CODE_INVALID_PARA;
×
2219
    }
2220
    if(len > TSDB_TABLE_NAME_LEN - 1) {
252,668✔
2221
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
470✔
2222
      len = TSDB_TABLE_NAME_LEN - 1;
470✔
2223
    }
2224

2225
    memcpy(tbname, pVal, len);
252,668✔
2226
    tbname[len] = '\0';  // ensure null terminated
252,668✔
2227
    if (NULL != strchr(tbname, '.')) {
252,910✔
UNCOV
2228
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
UNCOV
2229
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2230
    }
2231
  }
2232
  // TODO free dst
2233
  sclFreeParam(&dst);
256,812✔
2234
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
255,969✔
2235
  return code;
256,638✔
2236
}
2237

2238
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
263,529✔
2239
  if (pParam) {
263,529✔
2240
    if (pParam->tbname) {
263,529✔
2241
      taosMemFree(pParam->tbname);
263,529✔
2242
      pParam->tbname = NULL;
263,529✔
2243
    }
2244
    if (pParam->stbname) {
263,529✔
2245
      taosMemFree(pParam->stbname);
263,529✔
2246
      pParam->stbname = NULL;
263,529✔
2247
    }
2248
    if (pParam->dbFName) {
263,529✔
2249
      taosMemFree(pParam->dbFName);
263,529✔
2250
      pParam->dbFName = NULL;
263,529✔
2251
    }
2252
    if (pParam->pFields) {
263,529✔
2253
      taosArrayDestroy(pParam->pFields);
263,529✔
2254
      pParam->pFields = NULL;
263,529✔
2255
    }
2256
    if (pParam->pTagFields) {
263,529✔
2257
      taosArrayDestroy(pParam->pTagFields);
160,017✔
2258
      pParam->pTagFields = NULL;
160,017✔
2259
    }
2260
    if (pParam->colCids) {
263,529✔
2261
      taosArrayDestroy(pParam->colCids);
1,887✔
2262
      pParam->colCids = NULL;
1,887✔
2263
    }
2264
    if (pParam->tagCids) {
263,529✔
2265
      taosArrayDestroy(pParam->tagCids);
1,212✔
2266
      pParam->tagCids = NULL;
1,212✔
2267
    }
2268
    taosMemFree(pParam);
263,529✔
2269
  }
2270
}
263,529✔
2271

2272
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
263,529✔
2273
  int32_t code = 0, lino = 0;
263,529✔
2274
  if (ppDst == NULL || pSrc == NULL) {
263,529✔
UNCOV
2275
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2276
  }
2277
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
263,529✔
2278
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
263,529✔
2279

2280
  (*ppDst)->suid = pSrc->suid;
263,294✔
2281
  (*ppDst)->sver = pSrc->sver;
263,529✔
2282
  (*ppDst)->tbType = pSrc->tbType;
263,529✔
2283
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
263,529✔
2284
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
263,294✔
2285

2286
  if (pSrc->stbname) {
263,529✔
2287
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
263,529✔
2288
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
263,529✔
2289
  }
2290

2291
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
263,529✔
2292
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
263,529✔
2293

2294
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
263,294✔
2295

2296
  if (pSrc->pFields && pSrc->pFields->size > 0) {
263,529✔
2297
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
263,294✔
2298
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
263,035✔
2299
  } else {
2300
    (*ppDst)->pFields = NULL;
235✔
2301
  }
2302
  
2303
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
263,270✔
2304
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
159,758✔
2305
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
160,017✔
2306
  } else {
2307
    (*ppDst)->pTagFields = NULL;
103,512✔
2308
  }
2309

2310
  if (pSrc->colCids && pSrc->colCids->size > 0) {
263,529✔
2311
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,887✔
2312
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,887✔
2313
  } else {
2314
    (*ppDst)->colCids = NULL;
261,642✔
2315
  }
2316

2317
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
263,529✔
2318
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,212✔
2319
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,212✔
2320
  } else {
2321
    (*ppDst)->tagCids = NULL;
262,317✔
2322
  }
2323

2324
_exit:
263,529✔
2325

2326
  if (code != 0) {
263,529✔
UNCOV
2327
    if (*ppDst) {
×
UNCOV
2328
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2329
      *ppDst = NULL;
×
2330
    }
2331
    
2332
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2333
  }
2334
  return code;
263,529✔
2335
}
2336

UNCOV
2337
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
×
UNCOV
2338
  return doDropStreamTable(pMsgCb, pOutput, pReq);
×
2339
}
2340

2341
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2342
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2343
}
2344

2345
int32_t qFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
12,676✔
2346
  int32_t         code = TSDB_CODE_SUCCESS;
12,676✔
2347

2348
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
12,676✔
2349
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
12,676✔
2350
  if (code != TSDB_CODE_SUCCESS) {
12,676✔
UNCOV
2351
    goto end;
×
2352
  }
2353
end:
12,676✔
2354
  return code;
12,676✔
2355
}
2356

2357
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2358
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2359
    return true;
2,147,483,647✔
2360
  }
2361

2362
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2363
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2364
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2365
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2366
      return durationSatisfied;
2,147,483,647✔
2367
    case TRUE_FOR_COUNT_ONLY:
55,226,171✔
2368
      return countSatisfied;
55,226,171✔
2369
    case TRUE_FOR_AND:
55,226,171✔
2370
      return durationSatisfied && countSatisfied;
55,226,171✔
2371
    case TRUE_FOR_OR:
55,226,171✔
2372
      return durationSatisfied || countSatisfied;
55,226,171✔
UNCOV
2373
    default:
×
UNCOV
2374
      return true;
×
2375
  }
2376
}
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