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

taosdata / TDengine / #4947

02 Feb 2026 09:27AM UTC coverage: 66.872% (-0.06%) from 66.932%
#4947

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

3610 existing lines in 126 files now uncovered.

205539 of 307364 relevant lines covered (66.87%)

125933663.91 hits per line

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

69.67
/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 "tudf.h"
37
#include "wal.h"
38

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

43
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
579,819,234✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
579,819,234✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
579,819,234✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
579,894,848✔
47
}
579,811,205✔
48

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

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

65
static void cleanupRefPool() {
541,649✔
66
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
541,649✔
67
  taosCloseRef(ref);
541,649✔
68
}
541,649✔
69

70
static void initRefPool() {
541,649✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
541,649✔
72
  (void)atexit(cleanupRefPool);
541,649✔
73
}
541,649✔
74

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

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

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

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

128
    return TSDB_CODE_SUCCESS;
×
129
  }
130

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

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

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

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

156
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
49,512,045✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
49,512,045✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
49,513,308✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
22,663,608✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
22,664,206✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
22,663,882✔
162
      if (pScanInfo->base.dataReader != NULL) {
22,664,218✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
264,574✔
164
        if (code) {
264,574✔
165
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
166
          return code;
×
167
        }
168
      }
169
    }
170
  } else {
171
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
26,850,375✔
172
  }
173

174
  return 0;
26,318,629✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
26,318,312✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
26,318,312✔
179
  pTaskInfo->id.queryId = queryId;
26,318,312✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
26,318,611✔
181

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

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

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

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

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

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

209
  return code;
×
210
}
211

212
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
441,614✔
213
  if (msg == NULL) {  // create raw scan
441,614✔
214
    SExecTaskInfo* pTaskInfo = NULL;
115,767✔
215

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

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

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

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

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

248
  return pTaskInfo;
326,429✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
536,595✔
252
  if (streamInserterParam == NULL) {
536,595✔
253
    return TSDB_CODE_SUCCESS;
306,957✔
254
  }
255

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

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

266
  if (streamInserterParam->suid <= 0 &&
229,638✔
267
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
98,249✔
268
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
269
    return TSDB_CODE_INVALID_PARA;
×
270
  }
271

272
  return TSDB_CODE_SUCCESS;
229,638✔
273
}
274

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

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

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

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

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

330
_error:
72,337✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,118,113✔
342
  if (pInfo == NULL) {
3,118,113✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,118,113✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,118,113✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,118,113✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,118,113✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,854,673✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
458,489✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,972,786✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

362
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,659,624✔
363
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,659,624✔
364
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,659,624✔
365
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,659,624✔
366
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,659,624✔
367
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,659,624✔
368
}
369

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

377
  void*           info = pOperator->info;
3,118,113✔
378
  STableScanBase* pScanBaseInfo = NULL;
3,118,113✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,118,113✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
263,440✔
382
    setReadHandle(handle, pScanBaseInfo);
263,440✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,854,673✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,396,184✔
385
    setReadHandle(handle, pScanBaseInfo);
2,396,184✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
536,595✔
400

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

416
  return code;
536,595✔
417
}
418

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
389,746✔
433

434
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
389,746✔
435
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
389,746✔
436

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
389,746✔
444

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

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

454
  locked = 1;
389,746✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
927,812✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
538,066✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
538,066✔
458

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

465
    tDecoderClear(&mr.coder);
538,066✔
466

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

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

494
  pAPI->metaReaderFn.clearReader(&mr);
389,746✔
495
  locked = 0;
389,746✔
496

497
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
924,352✔
498
    bool     qualified = false;
534,606✔
499
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
534,606✔
500
    if (t == NULL) {
534,606✔
501
      continue;
×
502
    }
503

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

511
      if (!qualified) {
334,840✔
512
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
167,705✔
513
        continue;
167,705✔
514
      }
515
    }
516

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

521
  // handle multiple partition
522

523
_end:
389,746✔
524

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

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

538
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
390,385✔
539
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
390,385✔
540
  const char*    id = GET_TASKID(pTaskInfo);
390,385✔
541
  int32_t        code = 0;
390,385✔
542

543
  if (isAdd) {
390,385✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
389,746✔
545
  }
546

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
390,385✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
390,385✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
390,385✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
390,385✔
558
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
×
559
      int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
×
560

561
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
×
562
    }
563
  }
564

565
  if (isAdd) {  // add new table id
390,385✔
566
    SArray* qa = NULL;
389,746✔
567
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
389,746✔
568
    if (code != TSDB_CODE_SUCCESS) {
389,746✔
569
      taosArrayDestroy(qa);
×
570
      return code;
×
571
    }
572
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
389,746✔
573
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
389,746✔
574
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
389,746✔
575

576
    bool   assignUid = false;
389,746✔
577
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
389,746✔
578
    char*  keyBuf = NULL;
389,746✔
579
    if (bufLen > 0) {
389,746✔
580
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
581
      keyBuf = taosMemoryMalloc(bufLen);
×
582
      if (keyBuf == NULL) {
×
583
        taosArrayDestroy(qa);
×
584
        return terrno;
×
585
      }
586
    }
587

588
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
389,746✔
589
    taosWLockLatch(&pTaskInfo->lock);
389,746✔
590

591
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
756,647✔
592
      uint64_t* uid = taosArrayGet(qa, i);
366,901✔
593
      if (!uid) {
366,901✔
594
        taosMemoryFree(keyBuf);
×
595
        taosArrayDestroy(qa);
×
596
        taosWUnLockLatch(&pTaskInfo->lock);
×
597
        return terrno;
×
598
      }
599
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
366,901✔
600

601
      if (bufLen > 0) {
366,901✔
602
        if (assignUid) {
×
603
          keyInfo.groupId = keyInfo.uid;
×
604
        } else {
605
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
606
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
607
          if (code != TSDB_CODE_SUCCESS) {
×
608
            taosMemoryFree(keyBuf);
×
609
            taosArrayDestroy(qa);
×
610
            taosWUnLockLatch(&pTaskInfo->lock);
×
611
            return code;
×
612
          }
613
        }
614
      }
615

616
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
366,901✔
617
      if (code != TSDB_CODE_SUCCESS) {
366,901✔
618
        taosMemoryFree(keyBuf);
×
619
        taosArrayDestroy(qa);
×
620
        taosWUnLockLatch(&pTaskInfo->lock);
×
621
        return code;
×
622
      }
623
    }
624

625
    taosWUnLockLatch(&pTaskInfo->lock);
389,746✔
626
    if (keyBuf != NULL) {
389,746✔
627
      taosMemoryFree(keyBuf);
×
628
    }
629

630
    taosArrayDestroy(qa);
389,746✔
631
  } else {  // remove the table id in current list
632
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
639✔
633
    taosWLockLatch(&pTaskInfo->lock);
639✔
634
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
639✔
635
    taosWUnLockLatch(&pTaskInfo->lock);
639✔
636
  }
637

638
  return code;
390,385✔
639
}
640

641
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
454,173,626✔
642
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
643
                                    int32_t idx, bool* tbGet) {
644
  *tbGet = false;
454,173,626✔
645

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
454,187,403✔
647
    return TSDB_CODE_INVALID_PARA;
95✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
454,220,525✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
454,220,525✔
652
    return TSDB_CODE_SUCCESS;
265,882,378✔
653
  }
654

655
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
188,281,413✔
656
  if (!pSchemaInfo) {
188,168,214✔
657
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
658
    return terrno;
×
659
  }
660

661
  *sversion = pSchemaInfo->sw->version;
188,168,214✔
662
  *tversion = pSchemaInfo->tversion;
188,314,831✔
663
  *rversion = pSchemaInfo->rversion;
188,290,842✔
664
  if (pSchemaInfo->dbname) {
188,404,426✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
188,413,300✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
188,485,681✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
188,404,915✔
671
  } else {
672
    tableName[0] = 0;
977✔
673
  }
674

675
  *tbGet = true;
188,487,817✔
676

677
  return TSDB_CODE_SUCCESS;
188,384,345✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
265,826,956✔
681

682
void qDestroyOperatorParam(SOperatorParam* pParam) {
117,500✔
683
  if (NULL == pParam) {
117,500✔
684
    return;
×
685
  }
686
  freeOperatorParam(pParam, OP_GET_PARAM);
117,500✔
687
}
688

689
/**
690
  @brief Update the operator param for the task.
691
  @note  Unlike setOperatorParam, this function will destroy the new param when
692
         operator type mismatch.
693
*/
694
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
31,813,508✔
695
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
31,813,508✔
696
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
31,813,508✔
697
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
31,813,508✔
698
    qError("%s, %s operator type mismatch, task operator type:%d, "
115,150✔
699
           "new param operator type:%d", GET_TASKID(pTask), __func__,
700
           pTask->pRoot->operatorType,
701
           pNewParam->opType);
702
    qDestroyOperatorParam((SOperatorParam*)pParam);
115,150✔
703
    return;
115,150✔
704
  }
705
  TSWAP(pParam, pTask->pOpParam);
31,700,928✔
706
  ((SExecTaskInfo*)tinfo)->paramSet = false;
31,700,300✔
707
}
708

709
int32_t qExecutorInit(void) {
4,270,414✔
710
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,270,414✔
711
  return TSDB_CODE_SUCCESS;
4,270,507✔
712
}
713

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
2,899,419✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
2,899,419✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
2,899,419✔
717
  if (pTask->pWorkerCb) {
2,899,419✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
2,899,419✔
719
    if (code != TSDB_CODE_SUCCESS) {
2,905,139✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724

725
  code = tsem_wait(pSem);
2,909,299✔
726
  if (code != TSDB_CODE_SUCCESS) {
2,904,619✔
727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
728
    pTask->code = code;
×
729
    return pTask->code;
×
730
  }
731

732
  if (pTask->pWorkerCb) {
2,904,619✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
2,905,139✔
734
    if (code != TSDB_CODE_SUCCESS) {
2,905,139✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
2,904,619✔
740
}
741

742
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
268,260,466✔
743
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
744
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
745
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
268,260,466✔
746
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
268,260,466✔
747

748
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
268,257,704✔
749
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
750

751
  readHandle->uid = 0;
268,300,974✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
268,339,466✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
268,089,022✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
653,450✔
755
    goto _error;
443,310✔
756
  }
757
    
758
  if (handle) {
267,482,527✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
267,328,108✔
760
    void*           pSinkManager = NULL;
267,407,425✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
266,993,126✔
762
    if (code != TSDB_CODE_SUCCESS) {
267,318,174✔
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
267,318,174✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
267,334,658✔
769
    if (code != TSDB_CODE_SUCCESS) {
267,016,162✔
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
771
      taosMemoryFree(pSinkManager);
×
772
      goto _error;
×
773
    }
774

775
    SDataSinkNode* pSink = NULL;
267,016,162✔
776
    if (readHandle->localExec) {
267,262,369✔
777
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,816✔
778
      if (code != TSDB_CODE_SUCCESS) {
2,816✔
779
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
780
               (*pTask)->id.str);
781
        taosMemoryFree(pSinkManager);
×
782
        goto _error;
×
783
      }
784
    }
785

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
267,117,481✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
267,408,200✔
789
    if (code) {
267,084,071✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
579✔
791
    }
792
  }
793

794
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
267,506,424✔
795
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
796

797
_error:
50,924,863✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
268,264,942✔
800
}
801

802
static void freeBlock(void* param) {
×
803
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
804
  blockDataDestroy(pBlock);
×
805
}
×
806

807
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
314,624,055✔
808
                     bool processOneBlock) {
809
  int32_t        code = TSDB_CODE_SUCCESS;
314,624,055✔
810
  int32_t        lino = 0;
314,624,055✔
811
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
314,624,055✔
812
  int64_t        threadId = taosGetSelfPthreadId();
314,624,055✔
813

814
  if (pLocal) {
314,586,699✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
311,037,093✔
816
  }
817

818
  taosArrayClear(pResList);
314,598,808✔
819

820
  int64_t curOwner = 0;
314,510,480✔
821
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
314,510,480✔
822
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
823
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
824
    return pTaskInfo->code;
×
825
  }
826

827
  if (pTaskInfo->cost.start == 0) {
314,489,707✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
262,136,636✔
829
  }
830

831
  if (isTaskKilled(pTaskInfo)) {
314,679,891✔
UNCOV
832
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
833
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
UNCOV
834
    return pTaskInfo->code;
×
835
  }
836

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
314,488,305✔
839
  if (ret != TSDB_CODE_SUCCESS) {
314,920,578✔
840
    pTaskInfo->code = ret;
580,725✔
841
    (void)cleanUpUdfs();
580,725✔
842

843
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
580,725✔
844
    atomic_store_64(&pTaskInfo->owner, 0);
580,725✔
845

846
    return pTaskInfo->code;
580,725✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
314,339,853✔
850

851
  int32_t      current = 0;
314,346,476✔
852
  SSDataBlock* pRes = NULL;
314,346,476✔
853
  int64_t      st = taosGetTimestampUs();
314,586,245✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
314,586,245✔
856
    pTaskInfo->paramSet = true;
31,691,856✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
31,693,114✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
282,944,453✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
314,087,439✔
863
  code = blockDataCheck(pRes);
314,087,439✔
864
  QUERY_CHECK_CODE(code, lino, _end);
314,097,885✔
865

866
  if (pRes == NULL) {
314,097,885✔
867
    st = taosGetTimestampUs();
60,008,608✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
314,092,690✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
314,116,938✔
872
    rowsThreshold = 4096;
313,591,497✔
873
  }
874

875
  int32_t blockIndex = 0;
314,112,720✔
876
  while (pRes != NULL) {
813,440,441✔
877
    SSDataBlock* p = NULL;
543,753,149✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
543,724,499✔
879
      SSDataBlock* p1 = NULL;
423,218,734✔
880
      code = createOneDataBlock(pRes, true, &p1);
423,221,684✔
881
      QUERY_CHECK_CODE(code, lino, _end);
423,175,800✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
423,175,800✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
423,212,893✔
885
      p = p1;
423,212,893✔
886
    } else if (processOneBlock) {
120,536,653✔
887
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
16,302,133✔
888
      if (tmp) {
16,302,133✔
889
        blockDataDestroy(*tmp);
16,302,133✔
890
        *tmp = NULL;
16,302,133✔
891
      }
892
      SSDataBlock* p1 = NULL;
16,302,133✔
893
      code = createOneDataBlock(pRes, true, &p1);
16,302,133✔
894
      QUERY_CHECK_CODE(code, lino, _end);
16,302,133✔
895

896
      *tmp = p1;
16,302,133✔
897
      p = p1;
16,302,133✔
898
    } else {
899
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
104,234,520✔
900
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
104,235,403✔
901

902
      p = *(SSDataBlock**)tmp;
104,235,403✔
903
      code = copyDataBlock(p, pRes);
104,234,078✔
904
      QUERY_CHECK_CODE(code, lino, _end);
104,235,327✔
905
    }
906

907
    blockIndex += 1;
543,743,212✔
908

909
    current += p->info.rows;
543,743,212✔
910
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
543,750,740✔
911
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
912
    void* tmp = taosArrayPush(pResList, &p);
543,759,388✔
913
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
543,759,388✔
914

915
    if (current >= rowsThreshold || processOneBlock) {
543,759,388✔
916
      break;
917
    }
918

919
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
499,341,120✔
920
    QUERY_CHECK_CODE(code, lino, _end);
499,329,030✔
921
    code = blockDataCheck(pRes);
499,329,030✔
922
    QUERY_CHECK_CODE(code, lino, _end);
499,343,241✔
923
  }
924

925
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
314,105,560✔
926
    pTaskInfo->pSubplan->rowsThreshold -= current;
514,841✔
927
  }
928

929
  *hasMore = (pRes != NULL);
314,117,033✔
930
  uint64_t el = (taosGetTimestampUs() - st);
314,087,757✔
931

932
  pTaskInfo->cost.elapsedTime += el;
314,087,757✔
933
  if (NULL == pRes) {
314,085,838✔
934
    *useconds = pTaskInfo->cost.elapsedTime;
269,666,996✔
935
  }
936

937
_end:
314,127,109✔
938
  (void)cleanUpUdfs();
314,109,775✔
939

940
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
314,141,083✔
941
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
314,141,083✔
942
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
943

944
  atomic_store_64(&pTaskInfo->owner, 0);
314,141,075✔
945
  if (code) {
314,138,198✔
UNCOV
946
    pTaskInfo->code = code;
×
UNCOV
947
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
948
  }
949

950
  return pTaskInfo->code;
314,138,198✔
951
}
952

UNCOV
953
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
UNCOV
954
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
955
  SArray*        pList = pTaskInfo->pResultBlockList;
×
UNCOV
956
  size_t         num = taosArrayGetSize(pList);
×
UNCOV
957
  for (int32_t i = 0; i < num; ++i) {
×
958
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
959
    if (p) {
×
UNCOV
960
      blockDataDestroy(*p);
×
961
    }
962
  }
963

UNCOV
964
  taosArrayClear(pTaskInfo->pResultBlockList);
×
965
}
×
966

967
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
125,103,384✔
968
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
125,103,384✔
969
  int64_t        threadId = taosGetSelfPthreadId();
125,103,384✔
970
  int64_t        curOwner = 0;
125,104,752✔
971

972
  *pRes = NULL;
125,104,752✔
973

974
  // todo extract method
975
  taosRLockLatch(&pTaskInfo->lock);
125,104,752✔
976
  bool isKilled = isTaskKilled(pTaskInfo);
125,104,410✔
977
  if (isKilled) {
125,104,068✔
UNCOV
978
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
979

UNCOV
980
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
981
    return pTaskInfo->code;
×
982
  }
983

984
  if (pTaskInfo->owner != 0) {
125,104,068✔
UNCOV
985
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
986
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
987

UNCOV
988
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
989
    return pTaskInfo->code;
×
990
  }
991

992
  pTaskInfo->owner = threadId;
125,103,726✔
993
  taosRUnLockLatch(&pTaskInfo->lock);
125,104,068✔
994

995
  if (pTaskInfo->cost.start == 0) {
125,104,752✔
996
    pTaskInfo->cost.start = taosGetTimestampUs();
229,772✔
997
  }
998

999
  // error occurs, record the error code and return to client
1000
  int32_t ret = setjmp(pTaskInfo->env);
125,104,752✔
1001
  if (ret != TSDB_CODE_SUCCESS) {
125,101,674✔
UNCOV
1002
    pTaskInfo->code = ret;
×
UNCOV
1003
    (void)cleanUpUdfs();
×
UNCOV
1004
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
UNCOV
1005
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
1006
    return pTaskInfo->code;
×
1007
  }
1008

1009
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
125,101,674✔
1010

1011
  int64_t st = taosGetTimestampUs();
125,100,648✔
1012
  int32_t code = TSDB_CODE_SUCCESS;
125,100,648✔
1013
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
125,100,648✔
1014
    pTaskInfo->paramSet = true;
×
1015
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1016
  } else {
1017
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
125,100,990✔
1018
  }
1019
  if (code) {
125,070,623✔
UNCOV
1020
    pTaskInfo->code = code;
×
UNCOV
1021
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1022
  }
1023

1024
  code = blockDataCheck(*pRes);
125,070,623✔
1025
  if (code) {
125,099,519✔
1026
    pTaskInfo->code = code;
×
1027
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1028
  }
1029

1030
  uint64_t el = (taosGetTimestampUs() - st);
125,084,881✔
1031

1032
  pTaskInfo->cost.elapsedTime += el;
125,084,881✔
1033
  if (NULL == *pRes) {
125,087,648✔
1034
    *useconds = pTaskInfo->cost.elapsedTime;
22,258,985✔
1035
  }
1036

1037
  (void)cleanUpUdfs();
125,089,300✔
1038

1039
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
125,104,062✔
1040
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
125,103,720✔
1041

1042
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
125,104,752✔
1043
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1044

1045
  atomic_store_64(&pTaskInfo->owner, 0);
125,102,358✔
1046
  return pTaskInfo->code;
125,104,410✔
1047
}
1048

1049
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
100,546,590✔
1050
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
100,546,590✔
1051
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
100,548,246✔
1052
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
100,548,423✔
1053

1054
  if (!tmp) {
100,546,091✔
UNCOV
1055
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1056
    return terrno;
×
1057
  }
1058
  return TSDB_CODE_SUCCESS;
100,546,091✔
1059
}
1060

UNCOV
1061
int32_t stopInfoComp(void const* lp, void const* rp) {
×
UNCOV
1062
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
UNCOV
1063
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1064

UNCOV
1065
  if (key->refId < pInfo->refId) {
×
UNCOV
1066
    return -1;
×
1067
  } else if (key->refId > pInfo->refId) {
×
1068
    return 1;
×
1069
  }
1070

UNCOV
1071
  return 0;
×
1072
}
1073

1074
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1075
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
UNCOV
1076
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1077
  if (idx >= 0) {
×
1078
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1079
  }
1080
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
UNCOV
1081
}
×
1082

1083
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
49,766✔
1084
  if (pTaskInfo->subJobCtx.hasSubJobs) {
49,766✔
1085
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
10,400✔
1086
    if (pTaskInfo->subJobCtx.param) {
10,400✔
1087
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
5,720✔
1088
    }
1089
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
10,400✔
1090
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
10,400✔
1091
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
10,400✔
1092
  }
1093
  
1094
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
49,766✔
1095

1096
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
49,766✔
1097
  for (int32_t i = 0; i < num; ++i) {
51,099✔
1098
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
1,333✔
1099
    if (!pStop) {
1,333✔
UNCOV
1100
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1101
      continue;
×
1102
    }
1103
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
1,333✔
1104
    if (pExchangeInfo) {
1,333✔
1105
      int32_t code = tsem_post(&pExchangeInfo->ready);
1,333✔
1106
      if (code != TSDB_CODE_SUCCESS) {
1,333✔
UNCOV
1107
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1108
      } else {
1109
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
1,333✔
1110
      }
1111
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
1,333✔
1112
      if (code != TSDB_CODE_SUCCESS) {
1,333✔
1113
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1114
      }
1115
    }
1116
  }
1117

1118
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
49,766✔
1119
}
49,766✔
1120

1121
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
49,766✔
1122
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
49,766✔
1123
  if (pTaskInfo == NULL) {
49,766✔
UNCOV
1124
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1125
  }
1126

1127
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
49,766✔
1128

1129
  setTaskKilled(pTaskInfo, rspCode);
49,766✔
1130
  qStopTaskOperators(pTaskInfo);
49,766✔
1131

1132
  return TSDB_CODE_SUCCESS;
49,766✔
1133
}
1134

UNCOV
1135
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1136
  int64_t        st = taosGetTimestampMs();
×
UNCOV
1137
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
1138
  if (pTaskInfo == NULL) {
×
UNCOV
1139
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1140
  }
1141

UNCOV
1142
  if (waitDuration > 0) {
×
UNCOV
1143
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1144
  } else {
UNCOV
1145
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1146
  }
1147

1148
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1149

1150
  if (waitDuration > 0) {
×
1151
    while (1) {
UNCOV
1152
      taosWLockLatch(&pTaskInfo->lock);
×
UNCOV
1153
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1154
        taosWUnLockLatch(&pTaskInfo->lock);
×
1155

UNCOV
1156
        taosMsleep(200);
×
1157

UNCOV
1158
        int64_t d = taosGetTimestampMs() - st;
×
UNCOV
1159
        if (d >= waitDuration && waitDuration >= 0) {
×
1160
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
UNCOV
1161
          return TSDB_CODE_SUCCESS;
×
1162
        }
1163
      } else {  // not running now
1164
        pTaskInfo->code = rspCode;
×
1165
        taosWUnLockLatch(&pTaskInfo->lock);
×
1166
        return TSDB_CODE_SUCCESS;
×
1167
      }
1168
    }
1169
  }
1170

1171
  int64_t et = taosGetTimestampMs() - st;
×
1172
  if (et < waitDuration) {
×
1173
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
UNCOV
1174
    return TSDB_CODE_SUCCESS;
×
1175
  }
1176
  return TSDB_CODE_SUCCESS;
×
1177
}
1178

UNCOV
1179
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
UNCOV
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
UNCOV
1181
  if (NULL == pTaskInfo) {
×
UNCOV
1182
    return false;
×
1183
  }
1184

1185
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1186
}
1187

1188
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
268,556,919✔
1189
  STaskCostInfo* pSummary = &pTaskInfo->cost;
268,556,919✔
1190
  int64_t        idleTime = pSummary->start - pSummary->created;
268,564,507✔
1191

1192
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
268,565,720✔
1193
  if (pSummary->pRecoder != NULL) {
268,542,437✔
1194
    qDebug(
186,059,608✔
1195
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1196
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1197
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1198
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1199
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1200
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1201
  } else {
1202
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
82,467,935✔
1203
           pSummary->elapsedTime / 1000.0);
1204
  }
1205
}
268,527,543✔
1206

1207

1208
void qDestroyTask(qTaskInfo_t qTaskHandle) {
274,902,439✔
1209
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
274,902,439✔
1210
  if (pTaskInfo == NULL) {
274,902,439✔
1211
    return;
6,350,341✔
1212
  }
1213

1214
  if (pTaskInfo->pRoot != NULL) {
268,552,098✔
1215
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
268,560,810✔
1216
  } else {
UNCOV
1217
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1218
  }
1219

1220
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
268,564,098✔
1221
  doDestroyTask(pTaskInfo);
268,557,427✔
1222
}
1223

1224
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,634,121✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,634,121✔
1226
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,634,121✔
1227
}
1228

1229
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
326,429✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
326,429✔
1231
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
326,429✔
1232

1233
  while (1) {
320,197✔
1234
    uint16_t type = pOperator->operatorType;
646,626✔
1235
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
646,626✔
1236
      *scanner = pOperator->info;
326,429✔
1237
      break;
326,429✔
1238
    } else {
1239
      pOperator = pOperator->pDownstream[0];
320,197✔
1240
    }
1241
  }
1242
}
326,429✔
1243

1244
void* qExtractReaderFromTmqScanner(void* scanner) {
326,429✔
1245
  SStreamScanInfo* pInfo = scanner;
326,429✔
1246
  return (void*)pInfo->tqReader;
326,429✔
1247
}
1248

1249
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
718,192✔
1250
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
718,192✔
1251
  return pTaskInfo->streamInfo.schema;
718,192✔
1252
}
1253

1254
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
718,192✔
1255
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
718,192✔
1256
  return pTaskInfo->streamInfo.tbName;
718,192✔
1257
}
1258

1259
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
797,980✔
1260
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
797,980✔
1261
  return &pTaskInfo->streamInfo.btMetaRsp;
797,980✔
1262
}
1263

1264
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
23,315,153✔
1265
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,315,153✔
1266
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
23,315,153✔
1267
  return 0;
23,315,153✔
1268
  /*if (code != TSDB_CODE_SUCCESS) {
1269
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1270
    pTaskInfo->code = code;
1271
    T_LONG_JMP(pTaskInfo->env, code);
1272
  }*/
1273
}
1274

1275
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
762,361✔
1276
  memset(pCond, 0, sizeof(SQueryTableDataCond));
762,361✔
1277
  pCond->order = TSDB_ORDER_ASC;
762,361✔
1278
  pCond->numOfCols = pMtInfo->schema->nCols;
762,654✔
1279
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
763,546✔
1280
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
762,361✔
1281
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
762,068✔
1282
    taosMemoryFreeClear(pCond->colList);
599✔
UNCOV
1283
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1284
    return terrno;
×
1285
  }
1286

1287
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
761,775✔
1288
  pCond->suid = pMtInfo->suid;
762,068✔
1289
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
763,546✔
1290
  pCond->startVersion = -1;
763,546✔
1291
  pCond->endVersion = sContext->snapVersion;
762,361✔
1292

1293
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,859,185✔
1294
    SColumnInfo* pColInfo = &pCond->colList[i];
4,095,652✔
1295
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,095,359✔
1296
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,097,716✔
1297
    if (pMtInfo->pExtSchemas != NULL) {
4,095,932✔
1298
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,240✔
1299
    }
1300
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,096,811✔
1301
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,097,423✔
1302

1303
    pCond->pSlotList[i] = i;
4,096,238✔
1304
  }
1305

1306
  return TSDB_CODE_SUCCESS;
763,546✔
1307
}
1308

1309
void qStreamSetOpen(qTaskInfo_t tinfo) {
123,594,979✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
123,594,979✔
1311
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
123,594,979✔
1312
  pOperator->status = OP_NOT_OPENED;
123,606,019✔
1313
}
123,607,850✔
1314

1315
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
22,496,989✔
1316
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,496,989✔
1317
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
22,496,989✔
1318
}
22,496,997✔
1319

1320
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
23,475,733✔
1321
  int32_t        code = TSDB_CODE_SUCCESS;
23,475,733✔
1322
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
23,475,733✔
1323
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
23,475,733✔
1324

1325
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
23,476,710✔
1326
  const char*    id = GET_TASKID(pTaskInfo);
23,476,767✔
1327

1328
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
23,475,471✔
1329
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
22,199,520✔
1330
    if (pOperator == NULL || code != 0) {
22,198,191✔
UNCOV
1331
      return code;
×
1332
    }
1333

1334
    SStreamScanInfo* pInfo = pOperator->info;
22,198,527✔
1335
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
22,197,761✔
1336
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
22,198,490✔
1337
    walReaderVerifyOffset(pWalReader, pOffset);
22,199,108✔
1338
  }
1339
  // if pOffset equal to current offset, means continue consume
1340
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
23,473,015✔
1341
    return 0;
21,480,208✔
1342
  }
1343

1344
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,993,902✔
1345
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,214,150✔
1346
    if (pOperator == NULL || code != 0) {
1,213,536✔
UNCOV
1347
      return code;
×
1348
    }
1349

1350
    SStreamScanInfo* pInfo = pOperator->info;
1,213,851✔
1351
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,213,523✔
1352
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,213,840✔
1353
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,213,473✔
1354

1355
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,213,835✔
1356
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,013,426✔
1357
      pScanBaseInfo->dataReader = NULL;
1,013,366✔
1358

1359
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
1,013,081✔
1360
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
1,013,413✔
1361
      walReaderVerifyOffset(pWalReader, pOffset);
1,013,024✔
1362
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
1,014,010✔
1363
      if (code < 0) {
1,014,010✔
1364
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
10,126✔
1365
        return code;
10,126✔
1366
      }
1367
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
199,203✔
1368
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1369
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1370
      int64_t uid = pOffset->uid;
200,426✔
1371
      int64_t ts = pOffset->ts;
199,833✔
1372
      int32_t index = 0;
200,124✔
1373

1374
      // this value may be changed if new tables are created
1375
      taosRLockLatch(&pTaskInfo->lock);
200,124✔
1376
      int32_t numOfTables = 0;
200,738✔
1377
      code = tableListGetSize(pTableListInfo, &numOfTables);
200,738✔
1378
      if (code != TSDB_CODE_SUCCESS) {
199,811✔
UNCOV
1379
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1380
        taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1381
        return code;
×
1382
      }
1383

1384
      if (uid == 0) {
199,811✔
1385
        if (numOfTables != 0) {
193,780✔
1386
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,950✔
1387
          if (!tmp) {
39,263✔
UNCOV
1388
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1389
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1390
            return terrno;
×
1391
          }
1392
          if (tmp) uid = tmp->uid;
39,263✔
1393
          ts = INT64_MIN;
39,263✔
1394
          pScanInfo->currentTable = 0;
39,263✔
1395
        } else {
1396
          taosRUnLockLatch(&pTaskInfo->lock);
154,830✔
1397
          qError("no table in table list, %s", id);
155,444✔
1398
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
155,444✔
1399
        }
1400
      }
1401
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,294✔
1402

1403
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
45,609✔
1404
             pInfo->pTableScanOp->resultInfo.totalRows);
1405
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
45,609✔
1406

1407
      // start from current accessed position
1408
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1409
      // position, let's find it from the beginning.
1410
      index = tableListFind(pTableListInfo, uid, 0);
45,609✔
1411
      taosRUnLockLatch(&pTaskInfo->lock);
45,609✔
1412

1413
      if (index >= 0) {
45,609✔
1414
        pScanInfo->currentTable = index;
45,609✔
1415
      } else {
UNCOV
1416
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1417
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1418
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1419
      }
1420

1421
      STableKeyInfo keyInfo = {.uid = uid};
45,609✔
1422
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
45,609✔
1423

1424
      // let's start from the next ts that returned to consumer.
1425
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
45,609✔
1426
        pScanBaseInfo->cond.twindows.skey = ts;
903✔
1427
      } else {
1428
        pScanBaseInfo->cond.twindows.skey = ts + 1;
44,706✔
1429
      }
1430
      pScanInfo->scanTimes = 0;
45,609✔
1431

1432
      if (pScanBaseInfo->dataReader == NULL) {
45,609✔
1433
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
77,616✔
1434
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1435
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
38,808✔
1436
        if (code != TSDB_CODE_SUCCESS) {
38,808✔
UNCOV
1437
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
UNCOV
1438
          return code;
×
1439
        }
1440

1441
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
38,808✔
1442
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1443
      } else {
1444
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,801✔
1445
        if (code != TSDB_CODE_SUCCESS) {
6,493✔
UNCOV
1446
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1447
          return code;
×
1448
        }
1449

1450
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,493✔
1451
        if (code != TSDB_CODE_SUCCESS) {
6,801✔
UNCOV
1452
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1453
          return code;
×
1454
        }
1455
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
6,801✔
1456
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1457
      }
1458

1459
      // restore the key value
1460
      pScanBaseInfo->cond.twindows.skey = oldSkey;
45,609✔
1461
    } else {
UNCOV
1462
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
UNCOV
1463
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1464
    }
1465

1466
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1467
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
779,752✔
1468
      SStreamRawScanInfo* pInfo = pOperator->info;
764,391✔
1469
      SSnapContext*       sContext = pInfo->sContext;
764,391✔
1470
      SOperatorInfo*      p = NULL;
764,391✔
1471

1472
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
764,391✔
1473
      if (code != 0) {
764,085✔
1474
        return code;
×
1475
      }
1476

1477
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
764,085✔
1478

1479
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
764,391✔
UNCOV
1480
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
UNCOV
1481
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1482
      }
1483

1484
      SMetaTableInfo mtInfo = {0};
764,391✔
1485
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
764,391✔
1486
      if (code != 0) {
763,805✔
1487
        destroyMetaTableInfo(&mtInfo);
UNCOV
1488
        return code;
×
1489
      }
1490
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
763,805✔
1491
      pInfo->dataReader = NULL;
763,805✔
1492

1493
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
763,219✔
1494
      tableListClear(pTableListInfo);
762,913✔
1495

1496
      if (mtInfo.uid == 0) {
763,805✔
1497
        destroyMetaTableInfo(&mtInfo);
1498
        goto end;  // no data
845✔
1499
      }
1500

1501
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
762,960✔
1502
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
762,947✔
1503
      if (code != TSDB_CODE_SUCCESS) {
762,055✔
UNCOV
1504
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1505
        destroyMetaTableInfo(&mtInfo);
UNCOV
1506
        return code;
×
1507
      }
1508
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
762,055✔
1509
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,290✔
1510
      } else {
1511
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
761,364✔
1512
      }
1513

1514
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
763,240✔
1515
      if (code != TSDB_CODE_SUCCESS) {
763,546✔
1516
        destroyMetaTableInfo(&mtInfo);
UNCOV
1517
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1518
        return code;
×
1519
      }
1520

1521
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
763,546✔
1522
      if (!pList) {
763,546✔
UNCOV
1523
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1524
        destroyMetaTableInfo(&mtInfo);
UNCOV
1525
        return code;
×
1526
      }
1527
      int32_t size = 0;
763,546✔
1528
      code = tableListGetSize(pTableListInfo, &size);
763,546✔
1529
      if (code != TSDB_CODE_SUCCESS) {
763,546✔
1530
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1531
        destroyMetaTableInfo(&mtInfo);
UNCOV
1532
        return code;
×
1533
      }
1534

1535
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,527,092✔
1536
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
763,546✔
1537
      if (code != TSDB_CODE_SUCCESS) {
762,374✔
UNCOV
1538
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1539
        destroyMetaTableInfo(&mtInfo);
UNCOV
1540
        return code;
×
1541
      }
1542

1543
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
762,374✔
1544
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
762,960✔
1545
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1546
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
762,081✔
1547
      pTaskInfo->streamInfo.schema = mtInfo.schema;
762,081✔
1548
      taosMemoryFreeClear(mtInfo.pExtSchemas);
762,081✔
1549

1550
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
762,081✔
1551
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
15,361✔
1552
      SStreamRawScanInfo* pInfo = pOperator->info;
4,807✔
1553
      SSnapContext*       sContext = pInfo->sContext;
4,807✔
1554
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,807✔
1555
      if (code != 0) {
4,807✔
UNCOV
1556
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
UNCOV
1557
        return code;
×
1558
      }
1559
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,807✔
1560
             id);
1561
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
10,554✔
1562
      SStreamRawScanInfo* pInfo = pOperator->info;
10,554✔
1563
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
10,554✔
1564
      pInfo->dataReader = NULL;
10,554✔
1565
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
10,554✔
1566
    }
1567
  }
1568

1569
end:
1,828,831✔
1570
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,828,010✔
1571
  return 0;
1,828,939✔
1572
}
1573

1574
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
217,562,000✔
1575
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
217,562,000✔
1576
  if (pMsg->info.ahandle == NULL) {
217,600,845✔
1577
    rpcFreeCont(pMsg->pCont);
569✔
1578
    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));
569✔
1579
    return;
569✔
1580
  }
1581

1582
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
217,500,530✔
1583
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1584

1585
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
217,520,024✔
1586

1587
  if (pMsg->contLen > 0) {
217,564,544✔
1588
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
217,567,164✔
1589
    if (buf.pData == NULL) {
217,512,603✔
UNCOV
1590
      pMsg->code = terrno;
×
1591
    } else {
1592
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
217,512,603✔
1593
    }
1594
  }
1595

1596
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
217,575,377✔
1597
  rpcFreeCont(pMsg->pCont);
217,634,467✔
1598
  destroySendMsgInfo(pSendInfo);
217,583,909✔
1599
}
1600

UNCOV
1601
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1602
  int32_t        code = TSDB_CODE_SUCCESS;
×
UNCOV
1603
  int32_t        lino = 0;
×
UNCOV
1604
  SExecTaskInfo* pTaskInfo = tinfo;
×
UNCOV
1605
  SArray*        plist = NULL;
×
1606

UNCOV
1607
  code = getTableListInfo(pTaskInfo, &plist);
×
UNCOV
1608
  if (code || plist == NULL) {
×
UNCOV
1609
    return NULL;
×
1610
  }
1611

1612
  // only extract table in the first elements
1613
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1614

1615
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1616
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1617

UNCOV
1618
  int32_t numOfTables = 0;
×
1619
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1620
  QUERY_CHECK_CODE(code, lino, _end);
×
1621

UNCOV
1622
  for (int32_t i = 0; i < numOfTables; ++i) {
×
UNCOV
1623
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
UNCOV
1624
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1625
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
UNCOV
1626
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1627
  }
1628

UNCOV
1629
  taosArrayDestroy(plist);
×
1630

1631
_end:
×
1632
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1633
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1634
    T_LONG_JMP(pTaskInfo->env, code);
×
1635
  }
1636
  return pUidList;
×
1637
}
1638

1639
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,351,482✔
1640
  int32_t        code = TSDB_CODE_SUCCESS;
3,351,482✔
1641
  int32_t        lino = 0;
3,351,482✔
1642
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,351,482✔
1643

1644
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,352,023✔
1645
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1646
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1647

1648
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
UNCOV
1649
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1650
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,351,482✔
1651
    STableScanInfo* pScanInfo = pOperator->info;
1,675,741✔
1652

1653
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,675,741✔
1654
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,675,087✔
1655
  } else {
1656
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,675,741✔
1657
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,675,200✔
1658
    }
1659
  }
1660

1661
_end:
×
1662
  if (code != TSDB_CODE_SUCCESS) {
3,350,715✔
UNCOV
1663
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1664
  }
1665
  return code;
3,351,369✔
1666
}
1667

1668
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,675,741✔
1669
  if (pList == NULL) {
1,675,741✔
UNCOV
1670
    return TSDB_CODE_INVALID_PARA;
×
1671
  }
1672

1673
  *pList = NULL;
1,675,741✔
1674
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,675,741✔
1675
  if (pArray == NULL) {
1,675,087✔
UNCOV
1676
    return terrno;
×
1677
  }
1678

1679
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,675,087✔
1680
  if (code == 0) {
1,675,628✔
1681
    *pList = pArray;
1,675,628✔
1682
  } else {
UNCOV
1683
    taosArrayDestroy(pArray);
×
1684
  }
1685
  return code;
1,675,628✔
1686
}
1687

1688
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
UNCOV
1689
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1690
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
UNCOV
1691
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1692
  }
UNCOV
1693
  return 0;
×
1694
}
1695

UNCOV
1696
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
UNCOV
1697
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1698
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
UNCOV
1699
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1700
  }
1701
  return 0;
×
1702
}
1703

UNCOV
1704
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1705
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1706

UNCOV
1707
  int32_t code = pTaskInfo->code;
×
1708
  pTaskInfo->code = 0;
×
1709
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1710
}
×
1711

UNCOV
1712
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1713
  int32_t code = 0;
×
UNCOV
1714
  return code;
×
1715
}
1716

1717
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
UNCOV
1718
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1719
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1720
  return code;
×
1721
}
1722

1723
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
28,822,585✔
1724
  int32_t code = 0;
28,822,585✔
1725

1726
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
28,822,585✔
1727
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
28,820,425✔
1728

1729
  if (pOper->fpSet.resetStateFn) {
28,821,468✔
1730
    code = pOper->fpSet.resetStateFn(pOper);
28,820,817✔
1731
  }
1732
  pOper->status = OP_NOT_OPENED;
28,817,158✔
1733
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
49,566,960✔
1734
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,743,297✔
1735
  }
1736
  return code;
28,824,431✔
1737
}
1738

1739
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,079,736✔
1740
  int32_t        code = 0;
8,079,736✔
1741
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,079,736✔
1742
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,079,736✔
1743
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,079,736✔
1744
  code = clearStatesForOperator(pOper);
8,079,736✔
1745
  return code;
8,079,502✔
1746
}
1747

1748
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,989,807✔
1749
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,989,807✔
1750
  int64_t        threadId = taosGetSelfPthreadId();
10,989,807✔
1751
  int64_t        curOwner = 0;
10,989,807✔
1752

1753
  *ppRes = NULL;
10,989,807✔
1754

1755
  // todo extract method
1756
  taosRLockLatch(&pTaskInfo->lock);
10,989,807✔
1757
  bool isKilled = isTaskKilled(pTaskInfo);
10,989,807✔
1758
  if (isKilled) {
10,989,807✔
1759
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1760
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1761

UNCOV
1762
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1763
    return pTaskInfo->code;
×
1764
  }
1765

1766
  if (pTaskInfo->owner != 0) {
10,989,807✔
UNCOV
1767
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1768
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1769

UNCOV
1770
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1771
    return pTaskInfo->code;
×
1772
  }
1773

1774
  pTaskInfo->owner = threadId;
10,989,807✔
1775
  taosRUnLockLatch(&pTaskInfo->lock);
10,989,807✔
1776

1777
  if (pTaskInfo->cost.start == 0) {
10,989,807✔
1778
    pTaskInfo->cost.start = taosGetTimestampUs();
236,090✔
1779
  }
1780

1781
  // error occurs, record the error code and return to client
1782
  int32_t ret = setjmp(pTaskInfo->env);
10,989,807✔
1783
  if (ret != TSDB_CODE_SUCCESS) {
13,086,794✔
1784
    pTaskInfo->code = ret;
2,096,987✔
1785
    (void)cleanUpUdfs();
2,096,987✔
1786
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,096,987✔
1787
    atomic_store_64(&pTaskInfo->owner, 0);
2,096,987✔
1788
    return pTaskInfo->code;
2,096,987✔
1789
  }
1790

1791
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,989,807✔
1792

1793
  int64_t st = taosGetTimestampUs();
10,989,807✔
1794

1795
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,989,807✔
1796
  if (code) {
8,892,820✔
UNCOV
1797
    pTaskInfo->code = code;
×
UNCOV
1798
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1799
  } else {
1800
    *finished = *ppRes == NULL;
8,892,820✔
1801
    code = blockDataCheck(*ppRes);
8,892,820✔
1802
  }
1803
  if (code) {
8,892,820✔
UNCOV
1804
    pTaskInfo->code = code;
×
UNCOV
1805
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1806
  }
1807

1808
  uint64_t el = (taosGetTimestampUs() - st);
8,892,820✔
1809

1810
  pTaskInfo->cost.elapsedTime += el;
8,892,820✔
1811
  if (NULL == *ppRes) {
8,892,605✔
1812
    *useconds = pTaskInfo->cost.elapsedTime;
5,510,485✔
1813
  }
1814

1815
  (void)cleanUpUdfs();
8,892,605✔
1816

1817
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,892,820✔
1818
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,892,820✔
1819

1820
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,892,820✔
1821
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1822

1823
  atomic_store_64(&pTaskInfo->owner, 0);
8,892,820✔
1824
  return pTaskInfo->code;
8,892,605✔
1825
}
1826

1827
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1828
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1829
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1830
// }
1831

1832
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
249,766✔
1833
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1834
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1835
  int32_t code = 0;                                        
249,766✔
1836
  if (*pTableListInfo != NULL) {
249,766✔
UNCOV
1837
    qDebug("table list already exists, no need to create again");
×
UNCOV
1838
    goto end;
×
1839
  }
1840
  STableListInfo* pList = tableListCreate();
249,766✔
1841
  if (pList == NULL) {
249,556✔
UNCOV
1842
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1843
    code = terrno;
×
UNCOV
1844
    goto end;
×
1845
  }
1846

1847
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
249,556✔
1848
  SReadHandle    pHandle = {.vnode = pVnode};
250,275✔
1849
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
250,485✔
1850

1851
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
250,485✔
1852
  if (code != 0) {
250,485✔
UNCOV
1853
    tableListDestroy(pList);
×
1854
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1855
    goto end;
×
1856
  }
1857
  *pTableListInfo = pList;
250,485✔
1858

1859
end:
250,485✔
1860
  return 0;
250,485✔
1861
}
1862

1863
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
38,065✔
1864
  bool   listAdded = false;
38,065✔
1865
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
38,065✔
1866
  if (code == 0 && !listAdded) {
38,065✔
1867
    int32_t numOfTables = taosArrayGetSize(pUidList);
31,685✔
1868
    for (int i = 0; i < numOfTables; i++) {
92,818✔
1869
      void* tmp = taosArrayGet(pUidList, i);
61,133✔
1870
      if (tmp == NULL) {
61,133✔
UNCOV
1871
        return terrno;
×
1872
      }
1873
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
61,133✔
1874

1875
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
61,133✔
1876
      if (p == NULL) {
61,133✔
UNCOV
1877
        return terrno;
×
1878
      }
1879
    }
1880
  }
1881
  return code;
38,065✔
1882
}
1883

1884
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
29,973✔
1885
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1886
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1887
  int32_t code = TSDB_CODE_SUCCESS;
29,973✔
1888
  STableListInfo* pList = tableListCreate();
29,973✔
1889
  if (pList == NULL) {
29,973✔
UNCOV
1890
    code = terrno;
×
UNCOV
1891
    goto end;
×
1892
  }
1893
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
29,973✔
1894
  if (uidListCopy == NULL) {
29,973✔
UNCOV
1895
    code = terrno;
×
UNCOV
1896
    goto end;
×
1897
  }
1898
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
29,973✔
1899
  SReadHandle    pHandle = {.vnode = pVnode};
29,973✔
1900

1901
  pList->idInfo.suid = suid;
29,973✔
1902
  pList->idInfo.tableType = TD_SUPER_TABLE;
29,973✔
1903
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
29,973✔
1904
  if (code != TSDB_CODE_SUCCESS) {
29,973✔
UNCOV
1905
    goto end;
×
1906
  }                                              
1907
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
29,973✔
1908
  if (code != TSDB_CODE_SUCCESS) {
29,973✔
UNCOV
1909
    goto end;
×
1910
  }
1911
  *tableList = pList->pTableList;
29,973✔
1912
  pList->pTableList = NULL;
29,973✔
1913

1914
  taosArrayClear(uidList);
29,973✔
1915
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
59,946✔
1916
    void* tmp = taosArrayGet(uidListCopy, i);
29,973✔
1917
    if (tmp == NULL) {
29,973✔
UNCOV
1918
      continue;
×
1919
    }
1920
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
29,973✔
1921
    if (slot == NULL) {
29,973✔
1922
      if (taosArrayPush(uidList, tmp) == NULL) {
3,391✔
UNCOV
1923
        code = terrno;
×
UNCOV
1924
        goto end;
×
1925
      }
1926
    }
1927
  }
1928
end:
29,973✔
1929
  taosArrayDestroy(uidListCopy);
29,973✔
1930
  tableListDestroy(pList);
29,973✔
1931
  return code;
29,973✔
1932
}
1933

1934
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1935
//   int32_t index = -1;
1936
//   (void)taosThreadRwlockRdlock(lock);
1937
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1938
//     index = 0;
1939
//     goto end;
1940
//   }
1941
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1942
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1943

1944
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1945
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1946
//       index = i;
1947
//       goto end;
1948
//     }
1949
//   }
1950
// end:
1951
//   (void)taosThreadRwlockUnlock(lock);
1952
//   return index;
1953
// }
1954

1955
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
250,485✔
1956
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
250,277✔
1957
  STableListInfo* pList = pTableListInfo;
250,277✔
1958
  return pList->pTableList;
250,277✔
1959
}
1960

1961
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,285,215✔
1962

1963
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,499,795✔
1964
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,499,795✔
1965
  qDestroyTask(tInfo);
2,499,795✔
1966
}
2,499,795✔
1967

1968
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
580,847✔
1969
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
580,847✔
1970
}
1971

1972
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
628,495✔
1973
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1974
  int32_t      code = 0;
628,495✔
1975
  SNode*       pNode = 0;
628,495✔
1976
  SNodeList*   pList = NULL;
628,495✔
1977
  SExprInfo*   pExprInfo = NULL;
628,495✔
1978
  int32_t      numOfExprs = 1;
628,495✔
1979
  int32_t*     offset = 0;
628,495✔
1980
  STargetNode* pTargetNode = NULL;
628,495✔
1981
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
628,495✔
1982
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
628,495✔
1983

1984
  if (code == 0) {
628,495✔
1985
    pTargetNode->dataBlockId = 0;
628,495✔
1986
    pTargetNode->pExpr = pNode;
628,495✔
1987
    pTargetNode->slotId = 0;
628,495✔
1988
  }
1989
  if (code == 0) {
628,495✔
1990
    code = nodesMakeList(&pList);
628,495✔
1991
  }
1992
  if (code == 0) {
628,495✔
1993
    code = nodesListAppend(pList, (SNode*)pTargetNode);
628,495✔
1994
  }
1995
  if (code == 0) {
628,495✔
1996
    pNode = NULL;
628,495✔
1997
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
628,495✔
1998
  }
1999

2000
  if (code == 0) {
628,495✔
2001
    const char* pVal = NULL;
628,495✔
2002
    int32_t     len = 0;
628,495✔
2003
    SNode*      pSclNode = NULL;
628,495✔
2004
    switch (pExprInfo->pExpr->nodeType) {
628,495✔
2005
      case QUERY_NODE_FUNCTION:
628,495✔
2006
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
628,495✔
2007
        break;
628,495✔
UNCOV
2008
      case QUERY_NODE_OPERATOR:
×
UNCOV
2009
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
2010
        break;
×
UNCOV
2011
      default:
×
UNCOV
2012
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2013
        break;
×
2014
    }
2015
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
628,495✔
2016
    SSDataBlock block = {0};
628,495✔
2017
    block.info.rows = 1;
628,495✔
2018
    SSDataBlock* pBlock = &block;
628,495✔
2019
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
628,495✔
2020
    if (tmp == NULL) {
628,495✔
2021
      code = terrno;
×
2022
    }
2023
    if (code == 0) {
628,495✔
2024
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
628,495✔
2025
      gTaskScalarExtra.pStreamRange = NULL;
628,495✔
2026
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
628,495✔
2027
    }
2028
    taosArrayDestroy(pBlockList);
628,495✔
2029
  }
2030
  nodesDestroyList(pList);
628,495✔
2031
  destroyExprInfo(pExprInfo, numOfExprs);
628,495✔
2032
  taosMemoryFreeClear(pExprInfo);
628,495✔
2033
  return code;
628,495✔
2034
}
2035

2036
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,131,423✔
2037
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,131,423✔
2038
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,131,423✔
2039
  int32_t        code = 0;
2,131,423✔
2040
  SNode*         pNode = NULL;
2,131,423✔
2041
  if (!pForceOutputCols) return 0;
2,131,423✔
2042
  if (!*pRes) {
47,648✔
2043
    code = createDataBlock(pRes);
47,648✔
2044
  }
2045

2046
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
47,648✔
2047
    int32_t idx = 0;
47,648✔
2048
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
190,398✔
2049
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
142,750✔
2050
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
142,750✔
2051
      colInfo.info.precision = pCol->type.precision;
142,750✔
2052
      colInfo.info.scale = pCol->type.scale;
142,750✔
2053
      code = blockDataAppendColInfo(*pRes, &colInfo);
142,750✔
2054
      if (code != 0) break;
142,750✔
2055
    }
2056
  }
2057

2058
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
47,648✔
2059
  if (code != TSDB_CODE_SUCCESS) {
47,648✔
UNCOV
2060
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
2061
    return code;
×
2062
  }
2063

2064
  // loop all exprs for force output, execute all exprs
2065
  int32_t idx = 0;
47,648✔
2066
  int32_t rowIdx = (*pRes)->info.rows;
47,648✔
2067
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
47,648✔
2068
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
47,648✔
2069
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
190,398✔
2070
    SScalarParam   dst = {0};
142,750✔
2071
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
142,750✔
2072
    code = nodesStringToNode(pCol->expr, &pNode);
142,750✔
2073
    if (code != 0) break;
142,750✔
2074
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
142,750✔
2075
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
142,750✔
2076
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
95,102✔
2077
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
95,102✔
2078
    } else {
2079
      dst.columnData = pInfo;
47,648✔
2080
      dst.numOfRows = rowIdx;
47,648✔
2081
      dst.colAlloced = false;
47,648✔
2082
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
47,648✔
2083
    }
2084
    ++idx;
142,750✔
2085
    // TODO sclFreeParam(&dst);
2086
    nodesDestroyNode(pNode);
142,750✔
2087
    if (code != 0) break;
142,750✔
2088
  }
2089
  if (code == TSDB_CODE_SUCCESS) {
47,648✔
2090
    (*pRes)->info.rows++;
47,648✔
2091
  }
2092
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
47,648✔
2093
  return code;
47,648✔
2094
}
2095

2096
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
227,266✔
2097
  int32_t      code = 0;
227,266✔
2098
  const char*  pVal = NULL;
227,266✔
2099
  SScalarParam dst = {0};
227,266✔
2100
  int32_t      len = 0;
227,266✔
2101
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
227,266✔
2102
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
227,266✔
2103
  // execute the expr
2104
  switch (pExpr->type) {
227,266✔
UNCOV
2105
    case QUERY_NODE_VALUE: {
×
UNCOV
2106
      SValueNode* pValue = (SValueNode*)pExpr;
×
UNCOV
2107
      int32_t     type = pValue->node.resType.type;
×
UNCOV
2108
      if (!IS_STR_DATA_TYPE(type)) {
×
UNCOV
2109
        qError("invalid sub tb expr with non-str type");
×
UNCOV
2110
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2111
        break;
×
2112
      }
UNCOV
2113
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
UNCOV
2114
      if (pTmp == NULL) {
×
UNCOV
2115
        qError("invalid sub tb expr with null value");
×
UNCOV
2116
        code = TSDB_CODE_INVALID_PARA;
×
2117
        break;
×
2118
      }
2119
      pVal = varDataVal(pTmp);
×
2120
      len = varDataLen(pTmp);
×
2121
    } break;
×
2122
    case QUERY_NODE_FUNCTION: {
227,266✔
2123
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
227,266✔
2124
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
227,266✔
2125
        qError("invalid sub tb expr with non-str type func");
×
2126
        code = TSDB_CODE_INVALID_PARA;
×
2127
        break;
×
2128
      }
2129
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
227,266✔
2130
      if (!pCol) {
227,266✔
2131
        code = terrno;
×
2132
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2133
        break;
×
2134
      }
2135

2136
      pCol->hasNull = true;
227,266✔
2137
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
227,266✔
2138
      pCol->info.colId = 0;
227,266✔
2139
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
227,266✔
2140
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
227,266✔
2141
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
227,266✔
2142
      code = colInfoDataEnsureCapacity(pCol, 1, true);
227,266✔
2143
      if (code != 0) {
227,266✔
2144
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2145
        taosMemoryFree(pCol);
×
UNCOV
2146
        break;
×
2147
      }
2148
      dst.columnData = pCol;
227,266✔
2149
      dst.numOfRows = 1;
227,266✔
2150
      dst.colAlloced = true;
227,266✔
2151
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
227,266✔
2152
      if (colDataIsNull_var(dst.columnData, 0)) {
227,266✔
2153
        qInfo("invalid sub tb expr with null value");
2,296✔
2154
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,460✔
2155
      }
2156
      if (code == 0) {
227,266✔
2157
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
224,806✔
2158
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
224,806✔
2159
      }
2160
    } break;
227,020✔
UNCOV
2161
    default:
×
UNCOV
2162
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2163
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2164
      break;
×
2165
  }
2166
  if (code == 0) {
227,020✔
2167
    if (!pVal || len == 0) {
224,560✔
UNCOV
2168
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2169
      code = TSDB_CODE_INVALID_PARA;
×
2170
    }
2171
    if(len > TSDB_TABLE_NAME_LEN - 1) {
224,560✔
2172
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
430✔
2173
      len = TSDB_TABLE_NAME_LEN - 1;
430✔
2174
    }
2175

2176
    memcpy(tbname, pVal, len);
224,560✔
2177
    tbname[len] = '\0';  // ensure null terminated
224,560✔
2178
    if (NULL != strchr(tbname, '.')) {
224,806✔
UNCOV
2179
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2180
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2181
    }
2182
  }
2183
  // TODO free dst
2184
  sclFreeParam(&dst);
227,266✔
2185
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
227,266✔
2186
  return code;
227,266✔
2187
}
2188

2189
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
229,638✔
2190
  if (pParam) {
229,638✔
2191
    if (pParam->tbname) {
229,638✔
2192
      taosMemFree(pParam->tbname);
229,638✔
2193
      pParam->tbname = NULL;
229,638✔
2194
    }
2195
    if (pParam->stbname) {
229,638✔
2196
      taosMemFree(pParam->stbname);
229,638✔
2197
      pParam->stbname = NULL;
229,638✔
2198
    }
2199
    if (pParam->dbFName) {
229,638✔
2200
      taosMemFree(pParam->dbFName);
229,638✔
2201
      pParam->dbFName = NULL;
229,638✔
2202
    }
2203
    if (pParam->pFields) {
229,638✔
2204
      taosArrayDestroy(pParam->pFields);
229,638✔
2205
      pParam->pFields = NULL;
229,638✔
2206
    }
2207
    if (pParam->pTagFields) {
229,638✔
2208
      taosArrayDestroy(pParam->pTagFields);
131,389✔
2209
      pParam->pTagFields = NULL;
131,389✔
2210
    }
2211
    if (pParam->colCids) {
229,638✔
2212
      taosArrayDestroy(pParam->colCids);
1,731✔
2213
      pParam->colCids = NULL;
1,731✔
2214
    }
2215
    if (pParam->tagCids) {
229,638✔
2216
      taosArrayDestroy(pParam->tagCids);
1,108✔
2217
      pParam->tagCids = NULL;
1,108✔
2218
    }
2219
    taosMemFree(pParam);
229,638✔
2220
  }
2221
}
229,638✔
2222

2223
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
229,428✔
2224
  int32_t code = 0, lino = 0;
229,428✔
2225
  if (ppDst == NULL || pSrc == NULL) {
229,428✔
UNCOV
2226
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2227
  }
2228
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
229,428✔
2229
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
229,638✔
2230

2231
  (*ppDst)->suid = pSrc->suid;
229,638✔
2232
  (*ppDst)->sver = pSrc->sver;
229,638✔
2233
  (*ppDst)->tbType = pSrc->tbType;
229,233✔
2234
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
229,215✔
2235
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
229,638✔
2236

2237
  if (pSrc->stbname) {
229,428✔
2238
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
229,638✔
2239
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
229,638✔
2240
  }
2241

2242
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
229,638✔
2243
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
229,638✔
2244

2245
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
228,810✔
2246

2247
  if (pSrc->pFields && pSrc->pFields->size > 0) {
229,638✔
2248
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
229,638✔
2249
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
229,638✔
2250
  } else {
UNCOV
2251
    (*ppDst)->pFields = NULL;
×
2252
  }
2253
  
2254
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
229,638✔
2255
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
130,777✔
2256
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
131,389✔
2257
  } else {
2258
    (*ppDst)->pTagFields = NULL;
98,654✔
2259
  }
2260

2261
  if (pSrc->colCids && pSrc->colCids->size > 0) {
229,638✔
2262
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,731✔
2263
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,731✔
2264
  } else {
2265
    (*ppDst)->colCids = NULL;
227,907✔
2266
  }
2267

2268
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
229,638✔
2269
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,108✔
2270
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,108✔
2271
  } else {
2272
    (*ppDst)->tagCids = NULL;
227,702✔
2273
  }
2274

2275
_exit:
229,638✔
2276

2277
  if (code != 0) {
229,638✔
UNCOV
2278
    if (*ppDst) {
×
UNCOV
2279
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2280
      *ppDst = NULL;
×
2281
    }
2282
    
UNCOV
2283
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2284
  }
2285
  return code;
229,638✔
2286
}
2287

2288
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
246✔
2289
  return doDropStreamTable(pMsgCb, pOutput, pReq);
246✔
2290
}
2291

2292
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
UNCOV
2293
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2294
}
2295

2296
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
8,092✔
2297
  int32_t         code = TSDB_CODE_SUCCESS;
8,092✔
2298
  STableListInfo* pList = tableListCreate();
8,092✔
2299
  if (pList == NULL) {
8,092✔
UNCOV
2300
    code = terrno;
×
UNCOV
2301
    goto end;
×
2302
  }
2303
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2304
  // if (uidListCopy == NULL) {
2305
  //   code = terrno;
2306
  //   goto end;
2307
  // }
2308

2309
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
8,092✔
2310
  pList->idInfo.suid = suid;
8,092✔
2311
  pList->idInfo.tableType = TD_SUPER_TABLE;
8,092✔
2312
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
8,092✔
2313
  if (code != TSDB_CODE_SUCCESS) {
8,092✔
UNCOV
2314
    goto end;
×
2315
  }
2316
  // taosArrayClear(uidList);
2317
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2318
  //   void* tmp = taosArrayGet(uidListCopy, i);
2319
  //   if (tmp == NULL) {
2320
  //     continue;
2321
  //   }
2322
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2323
  //   if (slot == NULL) {
2324
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2325
  //       code = terrno;
2326
  //       goto end;
2327
  //     }
2328
  //   }
2329
  // }
2330
end:
8,092✔
2331
  // taosArrayDestroy(uidListCopy);
2332
  tableListDestroy(pList);
8,092✔
2333
  return code;
8,092✔
2334
}
2335

2336
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2337
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2338
    return true;
2,147,483,647✔
2339
  }
2340

2341
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2342
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2343
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2344
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2345
      return durationSatisfied;
2,147,483,647✔
2346
    case TRUE_FOR_COUNT_ONLY:
57,856✔
2347
      return countSatisfied;
57,856✔
2348
    case TRUE_FOR_AND:
57,856✔
2349
      return durationSatisfied && countSatisfied;
57,856✔
2350
    case TRUE_FOR_OR:
57,856✔
2351
      return durationSatisfied || countSatisfied;
57,856✔
UNCOV
2352
    default:
×
UNCOV
2353
      return true;
×
2354
  }
2355
}
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