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

taosdata / TDengine / #4930

16 Jan 2026 02:32AM UTC coverage: 66.716% (-0.007%) from 66.723%
#4930

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

1747 existing lines in 129 files now uncovered.

203203 of 304580 relevant lines covered (66.72%)

131619261.81 hits per line

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

69.4
/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) {
933,866,474✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
933,866,474✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
933,866,474✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
934,213,710✔
47
}
933,405,691✔
48

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

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

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

70
static void initRefPool() {
539,039✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
539,039✔
72
  (void)atexit(cleanupRefPool);
539,039✔
73
}
539,039✔
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) {
45,876,654✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
45,876,654✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
45,877,107✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
20,837,427✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
20,837,427✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
20,836,819✔
162
      if (pScanInfo->base.dataReader != NULL) {
20,836,109✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
263,709✔
164
        if (code) {
263,122✔
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);
25,039,255✔
172
  }
173

174
  return 0;
24,521,764✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
24,522,206✔
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) {
429,113✔
213
  if (msg == NULL) {  // create raw scan
429,113✔
214
    SExecTaskInfo* pTaskInfo = NULL;
109,939✔
215

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

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

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

232
  SSubplan* pPlan = NULL;
319,174✔
233
  int32_t   code = qStringToSubplan(msg, &pPlan);
321,090✔
234
  if (code != TSDB_CODE_SUCCESS) {
321,117✔
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;
321,117✔
241
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
321,117✔
242
  if (code != TSDB_CODE_SUCCESS) {
321,427✔
243
    qDestroyTask(pTaskInfo);
×
244
    terrno = code;
×
245
    return NULL;
×
246
  }
247

248
  return pTaskInfo;
321,427✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
527,592✔
252
  if (streamInserterParam == NULL) {
527,592✔
253
    return TSDB_CODE_SUCCESS;
302,833✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
224,759✔
273
}
274

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

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

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

310
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
224,544✔
311
    if (NULL == pInserterParam) {
224,759✔
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);
224,759✔
317
    TSDB_CHECK_CODE(code, lino, _error);
224,759✔
318
    
319
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
224,759✔
320
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
224,759✔
321

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
224,759✔
323
    if (code) {
224,759✔
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,
527,377✔
328
         tstrerror(code));
329

330
_error:
75,465✔
331

332
  if (code != TSDB_CODE_SUCCESS) {
527,377✔
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;
527,592✔
339
}
340

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,158,096✔
342
  if (pInfo == NULL) {
3,158,096✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,158,096✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,158,096✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,158,096✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,158,096✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,899,600✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
400,410✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
6,057,696✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,158,096✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
258,496✔
382
    setReadHandle(handle, pScanBaseInfo);
258,496✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,899,600✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,499,190✔
385
    setReadHandle(handle, pScanBaseInfo);
2,498,941✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
526,656✔
400

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

416
  return code;
527,592✔
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,
412,834✔
426
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
427
  int32_t code = TSDB_CODE_SUCCESS;
412,834✔
428
  int32_t lino = 0;
412,834✔
429
  int8_t  locked = 0;
412,834✔
430
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
412,834✔
431

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
412,834✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
412,834✔
444

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

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

454
  locked = 1;
412,834✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
1,188,269✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
775,435✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
775,435✔
458

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

465
    tDecoderClear(&mr.coder);
775,435✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
775,435✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
775,435✔
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) {
775,435✔
473
          continue;
3,420✔
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};
772,015✔
485
    if (pScanInfo->pTagCond != NULL) {
772,015✔
486
      // tb_uid_t id = mr.me.uid;
487
      item.check = 1;
334,510✔
488
    }
489
    if (taosArrayPush(tUid, &item) == NULL) {
772,015✔
490
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
491
    }
492
  }
493

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

497
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
1,184,849✔
498
    bool     qualified = false;
772,015✔
499
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
772,015✔
500
    if (t == NULL) {
772,015✔
501
      continue;
×
502
    }
503

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

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

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

521
  // handle multiple partition
522

523
_end:
412,834✔
524

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

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

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

543
  if (isAdd) {
414,838✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
412,834✔
545
  }
546

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

554
  SStreamScanInfo* pScanInfo = pInfo->info;
414,838✔
555
  STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
414,838✔
556
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE &&
414,838✔
557
      pTableScanInfo->base.metaCache.pTableMetaEntryCache) {  // clear meta cache for subscription if tag is changed
414,838✔
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
414,838✔
566
    SArray* qa = NULL;
412,834✔
567
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
412,834✔
568
    if (code != TSDB_CODE_SUCCESS) {
412,834✔
569
      taosArrayDestroy(qa);
×
570
      return code;
×
571
    }
572
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
412,834✔
573
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
412,834✔
574
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
412,834✔
575

576
    bool   assignUid = false;
412,834✔
577
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
412,834✔
578
    char*  keyBuf = NULL;
412,834✔
579
    if (bufLen > 0) {
412,834✔
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;
412,834✔
589
    taosWLockLatch(&pTaskInfo->lock);
412,834✔
590

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

601
      if (bufLen > 0) {
604,617✔
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);
604,617✔
617
      if (code != TSDB_CODE_SUCCESS) {
604,617✔
618
        taosMemoryFree(keyBuf);
×
619
        taosArrayDestroy(qa);
×
620
        taosWUnLockLatch(&pTaskInfo->lock);
×
621
        return code;
×
622
      }
623
    }
624

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

630
    taosArrayDestroy(qa);
412,834✔
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);
2,004✔
633
    taosWLockLatch(&pTaskInfo->lock);
2,004✔
634
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
2,004✔
635
    taosWUnLockLatch(&pTaskInfo->lock);
2,004✔
636
  }
637

638
  return code;
414,838✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
712,377,721✔
647
    return TSDB_CODE_INVALID_PARA;
38✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
712,623,878✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
712,623,878✔
652
    return TSDB_CODE_SUCCESS;
412,193,737✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
300,345,145✔
662
  *tversion = pSchemaInfo->tversion;
300,886,080✔
663
  *rversion = pSchemaInfo->rversion;
300,684,799✔
664
  if (pSchemaInfo->dbname) {
300,615,740✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
300,388,224✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
300,947,163✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
300,416,579✔
671
  } else {
672
    tableName[0] = 0;
171,154✔
673
  }
674

675
  *tbGet = true;
300,964,881✔
676

677
  return TSDB_CODE_SUCCESS;
300,864,670✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
411,774,812✔
681

682
void qDestroyOperatorParam(SOperatorParam* pParam) {
121,158✔
683
  if (NULL == pParam) {
121,158✔
684
    return;
×
685
  }
686
  freeOperatorParam(pParam, OP_GET_PARAM);
121,158✔
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) {
83,705,411✔
695
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
83,705,411✔
696
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
83,705,411✔
697
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
83,705,411✔
698
    qError("%s, %s operator type mismatch, task operator type:%d, "
117,342✔
699
           "new param operator type:%d", GET_TASKID(pTask), __func__,
700
           pTask->pRoot->operatorType,
701
           pNewParam->opType);
702
    qDestroyOperatorParam((SOperatorParam*)pParam);
117,342✔
703
    return;
117,342✔
704
  }
705
  TSWAP(pParam, pTask->pOpParam);
83,592,039✔
706
  ((SExecTaskInfo*)tinfo)->paramSet = false;
83,589,554✔
707
}
708

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

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
70,402,424✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
70,402,424✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
70,402,424✔
717
  if (pTask->pWorkerCb) {
70,402,424✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
70,417,354✔
719
    if (code != TSDB_CODE_SUCCESS) {
70,439,639✔
UNCOV
720
      pTask->code = code;
×
UNCOV
721
      return pTask->code;
×
722
    }
723
  }
724

725
  code = tsem_wait(pSem);
70,459,629✔
726
  if (code != TSDB_CODE_SUCCESS) {
70,442,115✔
UNCOV
727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
728
    pTask->code = code;
×
UNCOV
729
    return pTask->code;
×
730
  }
731

732
  if (pTask->pWorkerCb) {
70,442,115✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
70,446,337✔
734
    if (code != TSDB_CODE_SUCCESS) {
70,447,743✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
70,445,669✔
740
}
741

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

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

751
  readHandle->uid = 0;
425,494,670✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
425,590,580✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
425,006,978✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
12,042,421✔
755
    goto _error;
11,366,832✔
756
  }
757
    
758
  if (handle) {
413,179,250✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
413,456,362✔
760
    void*           pSinkManager = NULL;
413,640,731✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
413,255,255✔
762
    if (code != TSDB_CODE_SUCCESS) {
412,463,410✔
UNCOV
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
UNCOV
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
412,463,410✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
412,659,191✔
769
    if (code != TSDB_CODE_SUCCESS) {
412,750,395✔
UNCOV
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
UNCOV
771
      taosMemoryFree(pSinkManager);
×
UNCOV
772
      goto _error;
×
773
    }
774

775
    SDataSinkNode* pSink = NULL;
412,750,395✔
776
    if (readHandle->localExec) {
413,210,908✔
777
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,772✔
778
      if (code != TSDB_CODE_SUCCESS) {
2,772✔
779
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
780
               (*pTask)->id.str);
UNCOV
781
        taosMemoryFree(pSinkManager);
×
UNCOV
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,
412,682,252✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
413,599,324✔
789
    if (code) {
412,584,247✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
584✔
791
    }
792
  }
793

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

797
_error:
211,403,087✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
424,936,706✔
800
}
801

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

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

814
  if (pLocal) {
511,560,724✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
508,142,296✔
816
  }
817

818
  taosArrayClear(pResList);
511,138,172✔
819

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

827
  if (pTaskInfo->cost.start == 0) {
511,380,835✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
408,010,775✔
829
  }
830

831
  if (isTaskKilled(pTaskInfo)) {
511,639,069✔
832
    atomic_store_64(&pTaskInfo->owner, 0);
458✔
833
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
458✔
834
    return pTaskInfo->code;
458✔
835
  }
836

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
511,320,476✔
839
  if (ret != TSDB_CODE_SUCCESS) {
521,684,633✔
840
    pTaskInfo->code = ret;
11,065,541✔
841
    (void)cleanUpUdfs();
11,065,999✔
842

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

846
    return pTaskInfo->code;
11,066,915✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
510,619,092✔
850

851
  int32_t      current = 0;
510,621,507✔
852
  SSDataBlock* pRes = NULL;
510,621,507✔
853
  int64_t      st = taosGetTimestampUs();
511,656,517✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
511,656,517✔
856
    pTaskInfo->paramSet = true;
83,574,382✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
83,581,706✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
428,143,559✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
500,877,710✔
863
  code = blockDataCheck(pRes);
500,877,710✔
864
  QUERY_CHECK_CODE(code, lino, _end);
500,914,962✔
865

866
  if (pRes == NULL) {
500,914,962✔
867
    st = taosGetTimestampUs();
102,732,825✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
500,912,750✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
500,935,429✔
872
    rowsThreshold = 4096;
500,096,203✔
873
  }
874

875
  int32_t blockIndex = 0;
500,781,222✔
876
  while (pRes != NULL) {
1,144,903,026✔
877
    SSDataBlock* p = NULL;
695,917,057✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
695,791,734✔
879
      SSDataBlock* p1 = NULL;
532,450,071✔
880
      code = createOneDataBlock(pRes, true, &p1);
532,466,964✔
881
      QUERY_CHECK_CODE(code, lino, _end);
532,492,850✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
532,492,850✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
532,603,660✔
885
      p = p1;
532,603,660✔
886
    } else {
887
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
163,418,011✔
888
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
163,418,699✔
889

890
      p = *(SSDataBlock**)tmp;
163,418,699✔
891
      code = copyDataBlock(p, pRes);
163,417,644✔
892
      QUERY_CHECK_CODE(code, lino, _end);
163,419,238✔
893
    }
894

895
    blockIndex += 1;
695,981,134✔
896

897
    current += p->info.rows;
695,981,134✔
898
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
695,947,307✔
899
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
900
    void* tmp = taosArrayPush(pResList, &p);
696,008,385✔
901
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
696,008,385✔
902

903
    if (current >= rowsThreshold || processOneBlock) {
696,008,385✔
904
      break;
905
    }
906

907
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
644,026,940✔
908
    QUERY_CHECK_CODE(code, lino, _end);
644,024,448✔
909
    code = blockDataCheck(pRes);
644,024,448✔
910
    QUERY_CHECK_CODE(code, lino, _end);
644,054,502✔
911
  }
912

913
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
500,967,414✔
914
    pTaskInfo->pSubplan->rowsThreshold -= current;
538,060✔
915
  }
916

917
  *hasMore = (pRes != NULL);
501,019,039✔
918
  uint64_t el = (taosGetTimestampUs() - st);
500,885,904✔
919

920
  pTaskInfo->cost.elapsedTime += el;
500,885,904✔
921
  if (NULL == pRes) {
500,761,716✔
922
    *useconds = pTaskInfo->cost.elapsedTime;
448,780,271✔
923
  }
924

925
_end:
500,773,881✔
926
  (void)cleanUpUdfs();
500,917,456✔
927

928
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
501,110,490✔
929
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
501,110,840✔
930
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
931

932
  atomic_store_64(&pTaskInfo->owner, 0);
501,110,341✔
933
  if (code) {
501,107,326✔
UNCOV
934
    pTaskInfo->code = code;
×
UNCOV
935
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
936
  }
937

938
  return pTaskInfo->code;
501,107,326✔
939
}
940

UNCOV
941
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
UNCOV
942
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
943
  SArray*        pList = pTaskInfo->pResultBlockList;
×
UNCOV
944
  size_t         num = taosArrayGetSize(pList);
×
UNCOV
945
  for (int32_t i = 0; i < num; ++i) {
×
UNCOV
946
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
UNCOV
947
    if (p) {
×
UNCOV
948
      blockDataDestroy(*p);
×
949
    }
950
  }
951

UNCOV
952
  taosArrayClear(pTaskInfo->pResultBlockList);
×
UNCOV
953
}
×
954

955
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
132,634,830✔
956
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
132,634,830✔
957
  int64_t        threadId = taosGetSelfPthreadId();
132,634,830✔
958
  int64_t        curOwner = 0;
132,635,491✔
959

960
  *pRes = NULL;
132,635,491✔
961

962
  // todo extract method
963
  taosRLockLatch(&pTaskInfo->lock);
132,635,491✔
964
  bool isKilled = isTaskKilled(pTaskInfo);
132,636,517✔
965
  if (isKilled) {
132,636,508✔
UNCOV
966
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
967

968
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
969
    return pTaskInfo->code;
×
970
  }
971

972
  if (pTaskInfo->owner != 0) {
132,636,508✔
UNCOV
973
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
974
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
975

UNCOV
976
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
977
    return pTaskInfo->code;
×
978
  }
979

980
  pTaskInfo->owner = threadId;
132,635,824✔
981
  taosRUnLockLatch(&pTaskInfo->lock);
132,636,508✔
982

983
  if (pTaskInfo->cost.start == 0) {
132,636,901✔
984
    pTaskInfo->cost.start = taosGetTimestampUs();
227,044✔
985
  }
986

987
  // error occurs, record the error code and return to client
988
  int32_t ret = setjmp(pTaskInfo->env);
132,637,585✔
989
  if (ret != TSDB_CODE_SUCCESS) {
132,632,113✔
UNCOV
990
    pTaskInfo->code = ret;
×
991
    (void)cleanUpUdfs();
×
992
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
UNCOV
993
    atomic_store_64(&pTaskInfo->owner, 0);
×
UNCOV
994
    return pTaskInfo->code;
×
995
  }
996

997
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
132,632,113✔
998

999
  int64_t st = taosGetTimestampUs();
132,634,148✔
1000
  int32_t code = TSDB_CODE_SUCCESS;
132,634,148✔
1001
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
132,634,148✔
UNCOV
1002
    pTaskInfo->paramSet = true;
×
UNCOV
1003
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1004
  } else {
1005
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
132,630,028✔
1006
  }
1007
  if (code) {
132,581,303✔
1008
    pTaskInfo->code = code;
×
1009
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1010
  }
1011

1012
  code = blockDataCheck(*pRes);
132,581,303✔
1013
  if (code) {
132,623,434✔
UNCOV
1014
    pTaskInfo->code = code;
×
UNCOV
1015
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1016
  }
1017

1018
  uint64_t el = (taosGetTimestampUs() - st);
132,593,797✔
1019

1020
  pTaskInfo->cost.elapsedTime += el;
132,593,797✔
1021
  if (NULL == *pRes) {
132,608,472✔
1022
    *useconds = pTaskInfo->cost.elapsedTime;
20,164,059✔
1023
  }
1024

1025
  (void)cleanUpUdfs();
132,614,674✔
1026

1027
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
132,637,534✔
1028
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
132,637,534✔
1029

1030
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
132,637,534✔
1031
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1032

1033
  atomic_store_64(&pTaskInfo->owner, 0);
132,636,508✔
1034
  return pTaskInfo->code;
132,638,218✔
1035
}
1036

1037
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
129,545,840✔
1038
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
129,545,840✔
1039
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
129,552,868✔
1040
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
129,551,436✔
1041

1042
  if (!tmp) {
129,544,524✔
UNCOV
1043
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1044
    return terrno;
×
1045
  }
1046
  return TSDB_CODE_SUCCESS;
129,544,524✔
1047
}
1048

UNCOV
1049
int32_t stopInfoComp(void const* lp, void const* rp) {
×
UNCOV
1050
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
UNCOV
1051
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1052

UNCOV
1053
  if (key->refId < pInfo->refId) {
×
UNCOV
1054
    return -1;
×
UNCOV
1055
  } else if (key->refId > pInfo->refId) {
×
UNCOV
1056
    return 1;
×
1057
  }
1058

1059
  return 0;
×
1060
}
1061

UNCOV
1062
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
UNCOV
1063
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1064
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1065
  if (idx >= 0) {
×
1066
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1067
  }
1068
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1069
}
×
1070

1071
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
386,838✔
1072
  if (pTaskInfo->subJobCtx.hasSubJobs) {
386,838✔
1073
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
339,823✔
1074
    if (pTaskInfo->subJobCtx.param) {
339,823✔
1075
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
181,638✔
1076
    }
1077
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
339,823✔
1078
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
339,823✔
1079
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
339,823✔
1080
  }
1081
  
1082
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
386,838✔
1083

1084
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
386,838✔
1085
  for (int32_t i = 0; i < num; ++i) {
422,484✔
1086
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
35,646✔
1087
    if (!pStop) {
35,646✔
UNCOV
1088
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1089
      continue;
×
1090
    }
1091
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
35,646✔
1092
    if (pExchangeInfo) {
35,646✔
1093
      int32_t code = tsem_post(&pExchangeInfo->ready);
35,646✔
1094
      if (code != TSDB_CODE_SUCCESS) {
35,646✔
UNCOV
1095
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1096
      } else {
1097
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
35,646✔
1098
      }
1099
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
35,646✔
1100
      if (code != TSDB_CODE_SUCCESS) {
35,646✔
UNCOV
1101
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1102
      }
1103
    }
1104
  }
1105

1106
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
386,838✔
1107
}
386,838✔
1108

1109
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
386,838✔
1110
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
386,838✔
1111
  if (pTaskInfo == NULL) {
386,838✔
UNCOV
1112
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1113
  }
1114

1115
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
386,838✔
1116

1117
  setTaskKilled(pTaskInfo, rspCode);
386,838✔
1118
  qStopTaskOperators(pTaskInfo);
386,838✔
1119

1120
  return TSDB_CODE_SUCCESS;
386,838✔
1121
}
1122

UNCOV
1123
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
UNCOV
1124
  int64_t        st = taosGetTimestampMs();
×
UNCOV
1125
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
UNCOV
1126
  if (pTaskInfo == NULL) {
×
1127
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1128
  }
1129

UNCOV
1130
  if (waitDuration > 0) {
×
UNCOV
1131
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1132
  } else {
UNCOV
1133
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1134
  }
1135

UNCOV
1136
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1137

1138
  if (waitDuration > 0) {
×
1139
    while (1) {
1140
      taosWLockLatch(&pTaskInfo->lock);
×
1141
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1142
        taosWUnLockLatch(&pTaskInfo->lock);
×
1143

UNCOV
1144
        taosMsleep(200);
×
1145

1146
        int64_t d = taosGetTimestampMs() - st;
×
UNCOV
1147
        if (d >= waitDuration && waitDuration >= 0) {
×
1148
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
UNCOV
1149
          return TSDB_CODE_SUCCESS;
×
1150
        }
1151
      } else {  // not running now
UNCOV
1152
        pTaskInfo->code = rspCode;
×
1153
        taosWUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1154
        return TSDB_CODE_SUCCESS;
×
1155
      }
1156
    }
1157
  }
1158

1159
  int64_t et = taosGetTimestampMs() - st;
×
UNCOV
1160
  if (et < waitDuration) {
×
1161
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1162
    return TSDB_CODE_SUCCESS;
×
1163
  }
1164
  return TSDB_CODE_SUCCESS;
×
1165
}
1166

1167
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1168
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1169
  if (NULL == pTaskInfo) {
×
UNCOV
1170
    return false;
×
1171
  }
1172

UNCOV
1173
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1174
}
1175

1176
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
414,887,861✔
1177
  STaskCostInfo* pSummary = &pTaskInfo->cost;
414,887,861✔
1178
  int64_t        idleTime = pSummary->start - pSummary->created;
414,974,964✔
1179

1180
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
414,961,063✔
1181
  if (pSummary->pRecoder != NULL) {
414,789,573✔
1182
    qDebug(
296,389,110✔
1183
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1184
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1185
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1186
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1187
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1188
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1189
  } else {
1190
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
118,445,246✔
1191
           pSummary->elapsedTime / 1000.0);
1192
  }
1193
}
414,834,356✔
1194

1195

1196
void qDestroyTask(qTaskInfo_t qTaskHandle) {
432,111,062✔
1197
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
432,111,062✔
1198
  if (pTaskInfo == NULL) {
432,111,062✔
1199
    return;
17,245,706✔
1200
  }
1201

1202
  if (pTaskInfo->pRoot != NULL) {
414,865,356✔
1203
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
414,978,174✔
1204
  } else {
UNCOV
1205
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1206
  }
1207

1208
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
414,981,599✔
1209
  doDestroyTask(pTaskInfo);
414,886,279✔
1210
}
1211

1212
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
13,690,866✔
1213
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
13,690,866✔
1214
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
13,690,866✔
1215
}
1216

1217
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
321,427✔
1218
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
321,427✔
1219
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
321,427✔
1220

1221
  while (1) {
315,188✔
1222
    uint16_t type = pOperator->operatorType;
636,615✔
1223
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
636,917✔
1224
      *scanner = pOperator->info;
321,125✔
1225
      break;
321,125✔
1226
    } else {
1227
      pOperator = pOperator->pDownstream[0];
315,792✔
1228
    }
1229
  }
1230
}
321,125✔
1231

1232
void* qExtractReaderFromTmqScanner(void* scanner) {
321,125✔
1233
  SStreamScanInfo* pInfo = scanner;
321,125✔
1234
  return (void*)pInfo->tqReader;
321,125✔
1235
}
1236

1237
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
727,847✔
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
727,847✔
1239
  return pTaskInfo->streamInfo.schema;
727,847✔
1240
}
1241

1242
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
727,847✔
1243
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
727,847✔
1244
  return pTaskInfo->streamInfo.tbName;
727,847✔
1245
}
1246

1247
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
768,860✔
1248
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
768,860✔
1249
  return &pTaskInfo->streamInfo.btMetaRsp;
768,860✔
1250
}
1251

1252
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
21,448,013✔
1253
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,448,013✔
1254
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
21,448,013✔
1255
  return 0;
21,448,013✔
1256
  /*if (code != TSDB_CODE_SUCCESS) {
1257
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1258
    pTaskInfo->code = code;
1259
    T_LONG_JMP(pTaskInfo->env, code);
1260
  }*/
1261
}
1262

1263
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
727,724✔
1264
  memset(pCond, 0, sizeof(SQueryTableDataCond));
727,724✔
1265
  pCond->order = TSDB_ORDER_ASC;
727,724✔
1266
  pCond->numOfCols = pMtInfo->schema->nCols;
730,045✔
1267
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
732,047✔
1268
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
730,918✔
1269
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
731,507✔
1270
    taosMemoryFreeClear(pCond->colList);
2,037✔
UNCOV
1271
    taosMemoryFreeClear(pCond->pSlotList);
×
UNCOV
1272
    return terrno;
×
1273
  }
1274

1275
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
730,343✔
1276
  pCond->suid = pMtInfo->suid;
730,329✔
1277
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
730,301✔
1278
  pCond->startVersion = -1;
731,500✔
1279
  pCond->endVersion = sContext->snapVersion;
728,867✔
1280

1281
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,618,822✔
1282
    SColumnInfo* pColInfo = &pCond->colList[i];
3,887,080✔
1283
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,887,946✔
1284
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,889,685✔
1285
    if (pMtInfo->pExtSchemas != NULL) {
3,887,932✔
1286
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
45,760✔
1287
    }
1288
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,889,685✔
1289
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,886,484✔
1290

1291
    pCond->pSlotList[i] = i;
3,887,897✔
1292
  }
1293

1294
  return TSDB_CODE_SUCCESS;
732,671✔
1295
}
1296

1297
void qStreamSetOpen(qTaskInfo_t tinfo) {
131,145,753✔
1298
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
131,145,753✔
1299
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
131,145,753✔
1300
  pOperator->status = OP_NOT_OPENED;
131,159,388✔
1301
}
131,160,470✔
1302

1303
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
20,657,230✔
1304
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,657,230✔
1305
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
20,657,230✔
1306
}
20,659,447✔
1307

1308
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
21,618,056✔
1309
  int32_t        code = TSDB_CODE_SUCCESS;
21,618,056✔
1310
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,618,056✔
1311
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
21,618,056✔
1312

1313
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
21,619,365✔
1314
  const char*    id = GET_TASKID(pTaskInfo);
21,618,956✔
1315

1316
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
21,616,177✔
1317
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
20,361,564✔
1318
    if (pOperator == NULL || code != 0) {
20,358,347✔
UNCOV
1319
      return code;
×
1320
    }
1321

1322
    SStreamScanInfo* pInfo = pOperator->info;
20,358,959✔
1323
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
20,359,393✔
1324
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
20,358,121✔
1325
    walReaderVerifyOffset(pWalReader, pOffset);
20,361,606✔
1326
  }
1327
  // if pOffset equal to current offset, means continue consume
1328
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
21,616,310✔
1329
    return 0;
19,845,689✔
1330
  }
1331

1332
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,767,480✔
1333
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,020,080✔
1334
    if (pOperator == NULL || code != 0) {
1,019,805✔
UNCOV
1335
      return code;
×
1336
    }
1337

1338
    SStreamScanInfo* pInfo = pOperator->info;
1,019,805✔
1339
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,019,768✔
1340
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,019,523✔
1341
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,018,550✔
1342

1343
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,018,261✔
1344
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
810,690✔
1345
      pScanBaseInfo->dataReader = NULL;
810,690✔
1346

1347
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
810,690✔
1348
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
810,690✔
1349
      walReaderVerifyOffset(pWalReader, pOffset);
810,690✔
1350
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
810,690✔
1351
      if (code < 0) {
810,690✔
1352
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
11,428✔
1353
        return code;
11,428✔
1354
      }
1355
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
207,830✔
1356
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1357
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1358
      int64_t uid = pOffset->uid;
209,686✔
1359
      int64_t ts = pOffset->ts;
209,686✔
1360
      int32_t index = 0;
208,817✔
1361

1362
      // this value may be changed if new tables are created
1363
      taosRLockLatch(&pTaskInfo->lock);
208,817✔
1364
      int32_t numOfTables = 0;
208,750✔
1365
      code = tableListGetSize(pTableListInfo, &numOfTables);
208,750✔
1366
      if (code != TSDB_CODE_SUCCESS) {
207,224✔
UNCOV
1367
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1368
        taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1369
        return code;
×
1370
      }
1371

1372
      if (uid == 0) {
207,224✔
1373
        if (numOfTables != 0) {
199,959✔
1374
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,145✔
1375
          if (!tmp) {
38,453✔
UNCOV
1376
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1377
            taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1378
            return terrno;
×
1379
          }
1380
          if (tmp) uid = tmp->uid;
38,453✔
1381
          ts = INT64_MIN;
38,453✔
1382
          pScanInfo->currentTable = 0;
38,453✔
1383
        } else {
1384
          taosRUnLockLatch(&pTaskInfo->lock);
161,814✔
1385
          qError("no table in table list, %s", id);
162,683✔
1386
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
163,619✔
1387
        }
1388
      }
1389
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
45,718✔
1390

1391
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
46,067✔
1392
             pInfo->pTableScanOp->resultInfo.totalRows);
1393
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
46,067✔
1394

1395
      // start from current accessed position
1396
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1397
      // position, let's find it from the beginning.
1398
      index = tableListFind(pTableListInfo, uid, 0);
46,067✔
1399
      taosRUnLockLatch(&pTaskInfo->lock);
46,067✔
1400

1401
      if (index >= 0) {
46,067✔
1402
        pScanInfo->currentTable = index;
46,067✔
1403
      } else {
UNCOV
1404
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1405
               numOfTables, pScanInfo->currentTable, id);
UNCOV
1406
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1407
      }
1408

1409
      STableKeyInfo keyInfo = {.uid = uid};
46,067✔
1410
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
46,067✔
1411

1412
      // let's start from the next ts that returned to consumer.
1413
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
46,067✔
1414
        pScanBaseInfo->cond.twindows.skey = ts;
901✔
1415
      } else {
1416
        pScanBaseInfo->cond.twindows.skey = ts + 1;
45,166✔
1417
      }
1418
      pScanInfo->scanTimes = 0;
46,067✔
1419

1420
      if (pScanBaseInfo->dataReader == NULL) {
46,067✔
1421
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
79,280✔
1422
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1423
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
39,640✔
1424
        if (code != TSDB_CODE_SUCCESS) {
39,640✔
UNCOV
1425
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
UNCOV
1426
          return code;
×
1427
        }
1428

1429
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
39,640✔
1430
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1431
      } else {
1432
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,427✔
1433
        if (code != TSDB_CODE_SUCCESS) {
6,117✔
UNCOV
1434
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1435
          return code;
×
1436
        }
1437

1438
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,117✔
1439
        if (code != TSDB_CODE_SUCCESS) {
6,427✔
1440
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1441
          return code;
×
1442
        }
1443
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
6,427✔
1444
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1445
      }
1446

1447
      // restore the key value
1448
      pScanBaseInfo->cond.twindows.skey = oldSkey;
46,379✔
1449
    } else {
1450
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
UNCOV
1451
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1452
    }
1453

1454
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1455
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
747,400✔
1456
      SStreamRawScanInfo* pInfo = pOperator->info;
733,502✔
1457
      SSnapContext*       sContext = pInfo->sContext;
733,502✔
1458
      SOperatorInfo*      p = NULL;
733,502✔
1459

1460
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
733,502✔
1461
      if (code != 0) {
733,502✔
UNCOV
1462
        return code;
×
1463
      }
1464

1465
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
733,502✔
1466

1467
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
733,502✔
UNCOV
1468
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
UNCOV
1469
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1470
      }
1471

1472
      SMetaTableInfo mtInfo = {0};
733,502✔
1473
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
733,502✔
1474
      if (code != 0) {
732,338✔
1475
        destroyMetaTableInfo(&mtInfo);
UNCOV
1476
        return code;
×
1477
      }
1478
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
732,338✔
1479
      pInfo->dataReader = NULL;
730,915✔
1480

1481
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
731,465✔
1482
      tableListClear(pTableListInfo);
730,529✔
1483

1484
      if (mtInfo.uid == 0) {
732,920✔
1485
        destroyMetaTableInfo(&mtInfo);
1486
        goto end;  // no data
831✔
1487
      }
1488

1489
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
732,089✔
1490
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
730,925✔
1491
      if (code != TSDB_CODE_SUCCESS) {
732,671✔
UNCOV
1492
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1493
        destroyMetaTableInfo(&mtInfo);
UNCOV
1494
        return code;
×
1495
      }
1496
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
732,671✔
1497
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,285✔
1498
      } else {
1499
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
729,619✔
1500
      }
1501

1502
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
731,202✔
1503
      if (code != TSDB_CODE_SUCCESS) {
732,671✔
1504
        destroyMetaTableInfo(&mtInfo);
UNCOV
1505
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
UNCOV
1506
        return code;
×
1507
      }
1508

1509
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
732,671✔
1510
      if (!pList) {
732,671✔
UNCOV
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
UNCOV
1513
        return code;
×
1514
      }
1515
      int32_t size = 0;
732,671✔
1516
      code = tableListGetSize(pTableListInfo, &size);
732,671✔
1517
      if (code != TSDB_CODE_SUCCESS) {
732,671✔
UNCOV
1518
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1519
        destroyMetaTableInfo(&mtInfo);
1520
        return code;
×
1521
      }
1522

1523
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,465,342✔
1524
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
732,671✔
1525
      if (code != TSDB_CODE_SUCCESS) {
729,158✔
1526
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1527
        destroyMetaTableInfo(&mtInfo);
1528
        return code;
×
1529
      }
1530

1531
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
729,158✔
1532
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
730,911✔
1533
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1534
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
727,710✔
1535
      pTaskInfo->streamInfo.schema = mtInfo.schema;
730,038✔
1536
      taosMemoryFreeClear(mtInfo.pExtSchemas);
730,627✔
1537

1538
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
730,627✔
1539
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
13,607✔
1540
      SStreamRawScanInfo* pInfo = pOperator->info;
4,713✔
1541
      SSnapContext*       sContext = pInfo->sContext;
4,713✔
1542
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,713✔
1543
      if (code != 0) {
4,713✔
UNCOV
1544
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
UNCOV
1545
        return code;
×
1546
      }
1547
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,713✔
1548
             id);
1549
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,185✔
1550
      SStreamRawScanInfo* pInfo = pOperator->info;
9,185✔
1551
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,185✔
1552
      pInfo->dataReader = NULL;
9,185✔
1553
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,185✔
1554
    }
1555
  }
1556

1557
end:
1,592,623✔
1558
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,592,729✔
1559
  return 0;
1,592,729✔
1560
}
1561

1562
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
380,235,584✔
1563
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
380,235,584✔
1564
  if (pMsg->info.ahandle == NULL) {
380,364,355✔
1565
    rpcFreeCont(pMsg->pCont);
567✔
1566
    qError("pMsg->info.ahandle is NULL");
567✔
1567
    return;
567✔
1568
  }
1569

1570
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
380,107,535✔
1571
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1572

1573
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
380,113,676✔
1574

1575
  if (pMsg->contLen > 0) {
380,245,951✔
1576
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
380,112,848✔
1577
    if (buf.pData == NULL) {
379,992,786✔
UNCOV
1578
      pMsg->code = terrno;
×
1579
    } else {
1580
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
379,992,786✔
1581
    }
1582
  }
1583

1584
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
380,339,758✔
1585
  rpcFreeCont(pMsg->pCont);
380,417,510✔
1586
  destroySendMsgInfo(pSendInfo);
380,304,763✔
1587
}
1588

UNCOV
1589
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
UNCOV
1590
  int32_t        code = TSDB_CODE_SUCCESS;
×
UNCOV
1591
  int32_t        lino = 0;
×
UNCOV
1592
  SExecTaskInfo* pTaskInfo = tinfo;
×
1593
  SArray*        plist = NULL;
×
1594

UNCOV
1595
  code = getTableListInfo(pTaskInfo, &plist);
×
UNCOV
1596
  if (code || plist == NULL) {
×
UNCOV
1597
    return NULL;
×
1598
  }
1599

1600
  // only extract table in the first elements
UNCOV
1601
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1602

UNCOV
1603
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1604
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1605

1606
  int32_t numOfTables = 0;
×
1607
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1608
  QUERY_CHECK_CODE(code, lino, _end);
×
1609

1610
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1611
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1612
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
UNCOV
1613
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
UNCOV
1614
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1615
  }
1616

UNCOV
1617
  taosArrayDestroy(plist);
×
1618

1619
_end:
×
UNCOV
1620
  if (code != TSDB_CODE_SUCCESS) {
×
1621
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1622
    T_LONG_JMP(pTaskInfo->env, code);
×
1623
  }
UNCOV
1624
  return pUidList;
×
1625
}
1626

1627
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,355,664✔
1628
  int32_t        code = TSDB_CODE_SUCCESS;
3,355,664✔
1629
  int32_t        lino = 0;
3,355,664✔
1630
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,355,664✔
1631

1632
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,356,208✔
UNCOV
1633
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1634
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1635

1636
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1637
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1638
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,354,046✔
1639
    STableScanInfo* pScanInfo = pOperator->info;
1,677,832✔
1640

1641
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,677,832✔
1642
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,678,913✔
1643
  } else {
1644
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,678,369✔
1645
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,677,288✔
1646
    }
1647
  }
1648

1649
_end:
×
1650
  if (code != TSDB_CODE_SUCCESS) {
3,353,516✔
1651
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1652
  }
1653
  return code;
3,352,442✔
1654
}
1655

1656
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,677,832✔
1657
  if (pList == NULL) {
1,677,832✔
UNCOV
1658
    return TSDB_CODE_INVALID_PARA;
×
1659
  }
1660

1661
  *pList = NULL;
1,677,832✔
1662
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,677,832✔
1663
  if (pArray == NULL) {
1,678,369✔
1664
    return terrno;
×
1665
  }
1666

1667
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,678,369✔
1668
  if (code == 0) {
1,677,302✔
1669
    *pList = pArray;
1,677,302✔
1670
  } else {
UNCOV
1671
    taosArrayDestroy(pArray);
×
1672
  }
1673
  return code;
1,676,758✔
1674
}
1675

UNCOV
1676
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
UNCOV
1677
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
UNCOV
1678
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1679
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1680
  }
UNCOV
1681
  return 0;
×
1682
}
1683

UNCOV
1684
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
UNCOV
1685
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1686
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
UNCOV
1687
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1688
  }
UNCOV
1689
  return 0;
×
1690
}
1691

1692
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1693
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1694

UNCOV
1695
  int32_t code = pTaskInfo->code;
×
1696
  pTaskInfo->code = 0;
×
UNCOV
1697
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
UNCOV
1698
}
×
1699

1700
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1701
  int32_t code = 0;
×
1702
  return code;
×
1703
}
1704

UNCOV
1705
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
UNCOV
1706
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1707
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1708
  return code;
×
1709
}
1710

1711
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
24,585,161✔
1712
  int32_t code = 0;
24,585,161✔
1713

1714
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
24,585,161✔
1715
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
24,585,062✔
1716

1717
  if (pOper->fpSet.resetStateFn) {
24,585,270✔
1718
    code = pOper->fpSet.resetStateFn(pOper);
24,585,041✔
1719
  }
1720
  pOper->status = OP_NOT_OPENED;
24,583,553✔
1721
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
41,787,380✔
1722
    code = clearStatesForOperator(pOper->pDownstream[i]);
17,201,408✔
1723
  }
1724
  return code;
24,587,463✔
1725
}
1726

1727
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,383,816✔
1728
  int32_t        code = 0;
7,383,816✔
1729
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,383,816✔
1730
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,383,816✔
1731
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,384,031✔
1732
  code = clearStatesForOperator(pOper);
7,384,246✔
1733
  return code;
7,384,031✔
1734
}
1735

1736
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,456,569✔
1737
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,456,569✔
1738
  int64_t        threadId = taosGetSelfPthreadId();
10,456,569✔
1739
  int64_t        curOwner = 0;
10,456,388✔
1740

1741
  *ppRes = NULL;
10,456,388✔
1742

1743
  // todo extract method
1744
  taosRLockLatch(&pTaskInfo->lock);
10,456,388✔
1745
  bool isKilled = isTaskKilled(pTaskInfo);
10,457,164✔
1746
  if (isKilled) {
10,457,164✔
1747
    // clearStreamBlock(pTaskInfo->pRoot);
UNCOV
1748
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1749

UNCOV
1750
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1751
    return pTaskInfo->code;
×
1752
  }
1753

1754
  if (pTaskInfo->owner != 0) {
10,457,164✔
UNCOV
1755
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
UNCOV
1756
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1757

UNCOV
1758
    taosRUnLockLatch(&pTaskInfo->lock);
×
UNCOV
1759
    return pTaskInfo->code;
×
1760
  }
1761

1762
  pTaskInfo->owner = threadId;
10,457,164✔
1763
  taosRUnLockLatch(&pTaskInfo->lock);
10,457,392✔
1764

1765
  if (pTaskInfo->cost.start == 0) {
10,457,145✔
1766
    pTaskInfo->cost.start = taosGetTimestampUs();
231,119✔
1767
  }
1768

1769
  // error occurs, record the error code and return to client
1770
  int32_t ret = setjmp(pTaskInfo->env);
10,457,145✔
1771
  if (ret != TSDB_CODE_SUCCESS) {
11,898,409✔
1772
    pTaskInfo->code = ret;
1,442,574✔
1773
    (void)cleanUpUdfs();
1,442,574✔
1774
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,442,574✔
1775
    atomic_store_64(&pTaskInfo->owner, 0);
1,442,574✔
1776
    return pTaskInfo->code;
1,442,574✔
1777
  }
1778

1779
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,455,835✔
1780

1781
  int64_t st = taosGetTimestampUs();
10,457,177✔
1782

1783
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,457,177✔
1784
  if (code) {
9,014,173✔
UNCOV
1785
    pTaskInfo->code = code;
×
UNCOV
1786
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1787
  } else {
1788
    *finished = *ppRes == NULL;
9,014,173✔
1789
    code = blockDataCheck(*ppRes);
9,014,388✔
1790
  }
1791
  if (code) {
9,013,882✔
UNCOV
1792
    pTaskInfo->code = code;
×
UNCOV
1793
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1794
  }
1795

1796
  uint64_t el = (taosGetTimestampUs() - st);
9,013,743✔
1797

1798
  pTaskInfo->cost.elapsedTime += el;
9,013,743✔
1799
  if (NULL == *ppRes) {
9,014,173✔
1800
    *useconds = pTaskInfo->cost.elapsedTime;
5,528,791✔
1801
  }
1802

1803
  (void)cleanUpUdfs();
9,014,603✔
1804

1805
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
9,014,818✔
1806
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,014,818✔
1807

1808
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,014,818✔
1809
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1810

1811
  atomic_store_64(&pTaskInfo->owner, 0);
9,014,818✔
1812
  return pTaskInfo->code;
9,014,818✔
1813
}
1814

1815
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1816
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1817
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1818
// }
1819

1820
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
247,235✔
1821
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1822
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1823
  int32_t code = 0;                                        
247,235✔
1824
  if (*pTableListInfo != NULL) {
247,235✔
UNCOV
1825
    qDebug("table list already exists, no need to create again");
×
UNCOV
1826
    goto end;
×
1827
  }
1828
  STableListInfo* pList = tableListCreate();
247,235✔
1829
  if (pList == NULL) {
247,399✔
UNCOV
1830
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
1831
    code = terrno;
×
UNCOV
1832
    goto end;
×
1833
  }
1834

1835
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
247,399✔
1836
  SReadHandle    pHandle = {.vnode = pVnode};
247,399✔
1837
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
247,399✔
1838

1839
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
247,399✔
1840
  if (code != 0) {
247,399✔
1841
    tableListDestroy(pList);
×
UNCOV
1842
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
UNCOV
1843
    goto end;
×
1844
  }
1845
  *pTableListInfo = pList;
247,399✔
1846

1847
end:
247,399✔
1848
  return 0;
247,399✔
1849
}
1850

1851
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
41,288✔
1852
  bool   listAdded = false;
41,288✔
1853
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
41,288✔
1854
  if (code == 0 && !listAdded) {
41,288✔
1855
    int32_t numOfTables = taosArrayGetSize(pUidList);
35,397✔
1856
    for (int i = 0; i < numOfTables; i++) {
129,636✔
1857
      void* tmp = taosArrayGet(pUidList, i);
94,239✔
1858
      if (tmp == NULL) {
94,239✔
UNCOV
1859
        return terrno;
×
1860
      }
1861
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
94,239✔
1862

1863
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
94,239✔
1864
      if (p == NULL) {
94,239✔
UNCOV
1865
        return terrno;
×
1866
      }
1867
    }
1868
  }
1869
  return code;
41,288✔
1870
}
1871

1872
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
30,348✔
1873
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1874
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1875
  int32_t code = TSDB_CODE_SUCCESS;
30,348✔
1876
  STableListInfo* pList = tableListCreate();
30,348✔
1877
  if (pList == NULL) {
30,348✔
UNCOV
1878
    code = terrno;
×
UNCOV
1879
    goto end;
×
1880
  }
1881
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
30,348✔
1882
  if (uidListCopy == NULL) {
30,348✔
UNCOV
1883
    code = terrno;
×
UNCOV
1884
    goto end;
×
1885
  }
1886
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
30,348✔
1887
  SReadHandle    pHandle = {.vnode = pVnode};
30,348✔
1888

1889
  pList->idInfo.suid = suid;
30,133✔
1890
  pList->idInfo.tableType = TD_SUPER_TABLE;
30,348✔
1891
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
30,348✔
1892
  if (code != TSDB_CODE_SUCCESS) {
30,348✔
1893
    goto end;
×
1894
  }                                              
1895
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
30,348✔
1896
  if (code != TSDB_CODE_SUCCESS) {
30,348✔
UNCOV
1897
    goto end;
×
1898
  }
1899
  *tableList = pList->pTableList;
30,348✔
1900
  pList->pTableList = NULL;
30,348✔
1901

1902
  taosArrayClear(uidList);
30,348✔
1903
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
60,696✔
1904
    void* tmp = taosArrayGet(uidListCopy, i);
30,348✔
1905
    if (tmp == NULL) {
30,348✔
UNCOV
1906
      continue;
×
1907
    }
1908
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
30,348✔
1909
    if (slot == NULL) {
30,348✔
1910
      if (taosArrayPush(uidList, tmp) == NULL) {
3,289✔
UNCOV
1911
        code = terrno;
×
1912
        goto end;
×
1913
      }
1914
    }
1915
  }
1916
end:
30,348✔
1917
  taosArrayDestroy(uidListCopy);
30,348✔
1918
  tableListDestroy(pList);
30,348✔
1919
  return code;
30,348✔
1920
}
1921

1922
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1923
//   int32_t index = -1;
1924
//   (void)taosThreadRwlockRdlock(lock);
1925
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1926
//     index = 0;
1927
//     goto end;
1928
//   }
1929
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1930
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1931

1932
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1933
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1934
//       index = i;
1935
//       goto end;
1936
//     }
1937
//   }
1938
// end:
1939
//   (void)taosThreadRwlockUnlock(lock);
1940
//   return index;
1941
// }
1942

1943
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
247,399✔
1944
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
247,399✔
1945
  STableListInfo* pList = pTableListInfo;
247,399✔
1946
  return pList->pTableList;
247,399✔
1947
}
1948

1949
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,268,106✔
1950

1951
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,448,715✔
1952
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,448,715✔
1953
  qDestroyTask(tInfo);
2,448,715✔
1954
}
2,448,715✔
1955

1956
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
573,767✔
1957
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
573,767✔
1958
}
1959

1960
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
610,003✔
1961
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1962
  int32_t      code = 0;
610,003✔
1963
  SNode*       pNode = 0;
610,003✔
1964
  SNodeList*   pList = NULL;
610,003✔
1965
  SExprInfo*   pExprInfo = NULL;
609,756✔
1966
  int32_t      numOfExprs = 1;
609,756✔
1967
  int32_t*     offset = 0;
609,756✔
1968
  STargetNode* pTargetNode = NULL;
609,756✔
1969
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
609,756✔
1970
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
610,003✔
1971

1972
  if (code == 0) {
610,003✔
1973
    pTargetNode->dataBlockId = 0;
610,003✔
1974
    pTargetNode->pExpr = pNode;
610,003✔
1975
    pTargetNode->slotId = 0;
610,003✔
1976
  }
1977
  if (code == 0) {
610,003✔
1978
    code = nodesMakeList(&pList);
610,003✔
1979
  }
1980
  if (code == 0) {
610,003✔
1981
    code = nodesListAppend(pList, (SNode*)pTargetNode);
610,003✔
1982
  }
1983
  if (code == 0) {
610,003✔
1984
    pNode = NULL;
610,003✔
1985
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
610,003✔
1986
  }
1987

1988
  if (code == 0) {
610,003✔
1989
    const char* pVal = NULL;
610,003✔
1990
    int32_t     len = 0;
610,003✔
1991
    SNode*      pSclNode = NULL;
610,003✔
1992
    switch (pExprInfo->pExpr->nodeType) {
610,003✔
1993
      case QUERY_NODE_FUNCTION:
609,756✔
1994
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
609,756✔
1995
        break;
610,003✔
UNCOV
1996
      case QUERY_NODE_OPERATOR:
×
UNCOV
1997
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
UNCOV
1998
        break;
×
UNCOV
1999
      default:
×
UNCOV
2000
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2001
        break;
×
2002
    }
2003
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
610,003✔
2004
    SSDataBlock block = {0};
610,003✔
2005
    block.info.rows = 1;
610,003✔
2006
    SSDataBlock* pBlock = &block;
610,003✔
2007
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
609,756✔
2008
    if (tmp == NULL) {
609,756✔
UNCOV
2009
      code = terrno;
×
2010
    }
2011
    if (code == 0) {
609,756✔
2012
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
609,756✔
2013
      gTaskScalarExtra.pStreamRange = NULL;
609,756✔
2014
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
610,003✔
2015
    }
2016
    taosArrayDestroy(pBlockList);
610,003✔
2017
  }
2018
  nodesDestroyList(pList);
610,003✔
2019
  destroyExprInfo(pExprInfo, numOfExprs);
609,608✔
2020
  taosMemoryFreeClear(pExprInfo);
609,608✔
2021
  return code;
609,756✔
2022
}
2023

2024
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,046,334✔
2025
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,046,334✔
2026
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,046,334✔
2027
  int32_t        code = 0;
2,046,334✔
2028
  SNode*         pNode = NULL;
2,046,334✔
2029
  if (!pForceOutputCols) return 0;
2,046,334✔
2030
  if (!*pRes) {
36,236✔
2031
    code = createDataBlock(pRes);
36,236✔
2032
  }
2033

2034
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
36,236✔
2035
    int32_t idx = 0;
36,236✔
2036
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
144,758✔
2037
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
108,522✔
2038
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
108,522✔
2039
      colInfo.info.precision = pCol->type.precision;
108,522✔
2040
      colInfo.info.scale = pCol->type.scale;
108,522✔
2041
      code = blockDataAppendColInfo(*pRes, &colInfo);
108,522✔
2042
      if (code != 0) break;
108,522✔
2043
    }
2044
  }
2045

2046
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
36,236✔
2047
  if (code != TSDB_CODE_SUCCESS) {
36,236✔
UNCOV
2048
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
UNCOV
2049
    return code;
×
2050
  }
2051

2052
  // loop all exprs for force output, execute all exprs
2053
  int32_t idx = 0;
36,236✔
2054
  int32_t rowIdx = (*pRes)->info.rows;
36,236✔
2055
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
36,236✔
2056
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
36,236✔
2057
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
144,758✔
2058
    SScalarParam   dst = {0};
108,522✔
2059
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
108,522✔
2060
    code = nodesStringToNode(pCol->expr, &pNode);
108,522✔
2061
    if (code != 0) break;
108,522✔
2062
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
108,522✔
2063
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
108,522✔
2064
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
72,286✔
2065
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
72,286✔
2066
    } else {
2067
      dst.columnData = pInfo;
36,236✔
2068
      dst.numOfRows = rowIdx;
36,236✔
2069
      dst.colAlloced = false;
36,236✔
2070
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
36,236✔
2071
    }
2072
    ++idx;
108,522✔
2073
    // TODO sclFreeParam(&dst);
2074
    nodesDestroyNode(pNode);
108,522✔
2075
    if (code != 0) break;
108,522✔
2076
  }
2077
  if (code == TSDB_CODE_SUCCESS) {
36,236✔
2078
    (*pRes)->info.rows++;
36,236✔
2079
  }
2080
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
36,236✔
2081
  return code;
36,236✔
2082
}
2083

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

2124
      pCol->hasNull = true;
224,609✔
2125
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
224,609✔
2126
      pCol->info.colId = 0;
224,609✔
2127
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
224,609✔
2128
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
224,609✔
2129
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
224,362✔
2130
      code = colInfoDataEnsureCapacity(pCol, 1, true);
224,609✔
2131
      if (code != 0) {
224,609✔
UNCOV
2132
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
UNCOV
2133
        taosMemoryFree(pCol);
×
2134
        break;
×
2135
      }
2136
      dst.columnData = pCol;
224,609✔
2137
      dst.numOfRows = 1;
224,609✔
2138
      dst.colAlloced = true;
224,609✔
2139
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
224,609✔
2140
      if (colDataIsNull_var(dst.columnData, 0)) {
224,214✔
2141
        qInfo("invalid sub tb expr with null value");
2,460✔
2142
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,460✔
2143
      }
2144
      if (code == 0) {
223,967✔
2145
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
221,507✔
2146
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
221,507✔
2147
      }
2148
    } break;
224,150✔
2149
    default:
×
UNCOV
2150
      qError("wrong subtable expr with type: %d", pExpr->type);
×
UNCOV
2151
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
UNCOV
2152
      break;
×
2153
  }
2154
  if (code == 0) {
224,150✔
2155
    if (!pVal || len == 0) {
221,754✔
UNCOV
2156
      qError("tbname generated with no characters which is not allowed");
×
UNCOV
2157
      code = TSDB_CODE_INVALID_PARA;
×
2158
    }
2159
    if(len > TSDB_TABLE_NAME_LEN - 1) {
221,754✔
2160
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
430✔
2161
      len = TSDB_TABLE_NAME_LEN - 1;
430✔
2162
    }
2163

2164
    memcpy(tbname, pVal, len);
221,754✔
2165
    tbname[len] = '\0';  // ensure null terminated
221,754✔
2166
    if (NULL != strchr(tbname, '.')) {
221,937✔
2167
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
UNCOV
2168
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2169
    }
2170
  }
2171
  // TODO free dst
2172
  sclFreeParam(&dst);
224,333✔
2173
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
224,214✔
2174
  return code;
224,609✔
2175
}
2176

2177
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
224,759✔
2178
  if (pParam) {
224,759✔
2179
    if (pParam->tbname) {
224,759✔
2180
      taosMemFree(pParam->tbname);
224,759✔
2181
      pParam->tbname = NULL;
224,759✔
2182
    }
2183
    if (pParam->stbname) {
224,759✔
2184
      taosMemFree(pParam->stbname);
224,759✔
2185
      pParam->stbname = NULL;
224,759✔
2186
    }
2187
    if (pParam->dbFName) {
224,759✔
2188
      taosMemFree(pParam->dbFName);
224,759✔
2189
      pParam->dbFName = NULL;
224,759✔
2190
    }
2191
    if (pParam->pFields) {
224,759✔
2192
      taosArrayDestroy(pParam->pFields);
224,759✔
2193
      pParam->pFields = NULL;
224,759✔
2194
    }
2195
    if (pParam->pTagFields) {
224,759✔
2196
      taosArrayDestroy(pParam->pTagFields);
131,627✔
2197
      pParam->pTagFields = NULL;
131,627✔
2198
    }
2199
    if (pParam->colCids) {
224,759✔
2200
      taosArrayDestroy(pParam->colCids);
1,725✔
2201
      pParam->colCids = NULL;
1,725✔
2202
    }
2203
    if (pParam->tagCids) {
224,759✔
2204
      taosArrayDestroy(pParam->tagCids);
1,108✔
2205
      pParam->tagCids = NULL;
1,108✔
2206
    }
2207
    taosMemFree(pParam);
224,759✔
2208
  }
2209
}
224,759✔
2210

2211
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
224,759✔
2212
  int32_t code = 0, lino = 0;
224,759✔
2213
  if (ppDst == NULL || pSrc == NULL) {
224,759✔
UNCOV
2214
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2215
  }
2216
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
224,759✔
2217
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
224,544✔
2218

2219
  (*ppDst)->suid = pSrc->suid;
224,544✔
2220
  (*ppDst)->sver = pSrc->sver;
224,759✔
2221
  (*ppDst)->tbType = pSrc->tbType;
224,759✔
2222
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
224,759✔
2223
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
224,544✔
2224

2225
  if (pSrc->stbname) {
224,759✔
2226
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
224,544✔
2227
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
224,759✔
2228
  }
2229

2230
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
224,974✔
2231
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
224,759✔
2232

2233
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
224,544✔
2234

2235
  if (pSrc->pFields && pSrc->pFields->size > 0) {
224,544✔
2236
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
224,759✔
2237
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
224,759✔
2238
  } else {
UNCOV
2239
    (*ppDst)->pFields = NULL;
×
2240
  }
2241
  
2242
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
224,759✔
2243
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
131,627✔
2244
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
131,627✔
2245
  } else {
2246
    (*ppDst)->pTagFields = NULL;
93,132✔
2247
  }
2248

2249
  if (pSrc->colCids && pSrc->colCids->size > 0) {
224,759✔
2250
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,725✔
2251
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,725✔
2252
  } else {
2253
    (*ppDst)->colCids = NULL;
223,034✔
2254
  }
2255

2256
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
224,759✔
2257
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,108✔
2258
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,108✔
2259
  } else {
2260
    (*ppDst)->tagCids = NULL;
223,651✔
2261
  }
2262

2263
_exit:
224,759✔
2264

2265
  if (code != 0) {
224,759✔
UNCOV
2266
    if (*ppDst) {
×
UNCOV
2267
      destroyStreamInserterParam(*ppDst);
×
UNCOV
2268
      *ppDst = NULL;
×
2269
    }
2270
    
UNCOV
2271
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2272
  }
2273
  return code;
224,759✔
2274
}
2275

2276
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
247✔
2277
  return doDropStreamTable(pMsgCb, pOutput, pReq);
247✔
2278
}
2279

UNCOV
2280
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2281
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2282
}
2283

2284
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
10,940✔
2285
  int32_t         code = TSDB_CODE_SUCCESS;
10,940✔
2286
  STableListInfo* pList = tableListCreate();
10,940✔
2287
  if (pList == NULL) {
10,940✔
UNCOV
2288
    code = terrno;
×
UNCOV
2289
    goto end;
×
2290
  }
2291
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2292
  // if (uidListCopy == NULL) {
2293
  //   code = terrno;
2294
  //   goto end;
2295
  // }
2296

2297
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
10,940✔
2298
  pList->idInfo.suid = suid;
10,940✔
2299
  pList->idInfo.tableType = TD_SUPER_TABLE;
10,940✔
2300
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
10,940✔
2301
  if (code != TSDB_CODE_SUCCESS) {
10,940✔
UNCOV
2302
    goto end;
×
2303
  }
2304
  // taosArrayClear(uidList);
2305
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2306
  //   void* tmp = taosArrayGet(uidListCopy, i);
2307
  //   if (tmp == NULL) {
2308
  //     continue;
2309
  //   }
2310
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2311
  //   if (slot == NULL) {
2312
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2313
  //       code = terrno;
2314
  //       goto end;
2315
  //     }
2316
  //   }
2317
  // }
2318
end:
10,940✔
2319
  // taosArrayDestroy(uidListCopy);
2320
  tableListDestroy(pList);
10,940✔
2321
  return code;
10,940✔
2322
}
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