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

taosdata / TDengine / #4936

23 Jan 2026 09:40AM UTC coverage: 66.746% (+0.04%) from 66.708%
#4936

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204023 of 305671 relevant lines covered (66.75%)

124768167.97 hits per line

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

68.92
/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) {
612,476,391✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
612,476,391✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
612,476,391✔
46
  gTaskScalarExtra.fp = qFetchRemoteNode;
612,567,612✔
47
}
612,289,123✔
48

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

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

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

70
static void initRefPool() {
539,115✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
539,115✔
72
  (void)atexit(cleanupRefPool);
539,115✔
73
}
539,115✔
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) {
47,439,514✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
47,439,514✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
47,442,227✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
21,621,474✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
21,621,474✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
21,621,186✔
162
      if (pScanInfo->base.dataReader != NULL) {
21,621,186✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
263,849✔
164
        if (code) {
262,960✔
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,818,369✔
172
  }
173

174
  return 0;
25,282,911✔
175
}
176

177
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
25,282,827✔
178
  SExecTaskInfo* pTaskInfo = tinfo;
25,282,827✔
179
  pTaskInfo->id.queryId = queryId;
25,282,827✔
180
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
25,283,118✔
181

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
25,282,174✔
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) {
443,173✔
213
  if (msg == NULL) {  // create raw scan
443,173✔
214
    SExecTaskInfo* pTaskInfo = NULL;
119,065✔
215

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

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

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

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

248
  return pTaskInfo;
324,357✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
572,523✔
252
  if (streamInserterParam == NULL) {
572,523✔
253
    return TSDB_CODE_SUCCESS;
352,233✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
220,290✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
220,290✔
323
    if (code) {
220,290✔
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,
572,523✔
328
         tstrerror(code));
329

330
_error:
72,093✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,090,022✔
342
  if (pInfo == NULL) {
3,090,022✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,090,022✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,090,022✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,090,022✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,090,022✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,819,780✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
472,452✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,909,802✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,090,022✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
270,242✔
382
    setReadHandle(handle, pScanBaseInfo);
270,242✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,819,780✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,347,328✔
385
    setReadHandle(handle, pScanBaseInfo);
2,347,328✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
572,523✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
381,309✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
381,309✔
444

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

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

454
  locked = 1;
381,309✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
863,796✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
482,487✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
482,487✔
458

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

465
    tDecoderClear(&mr.coder);
482,487✔
466

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
381,309✔
524

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

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

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

543
  if (isAdd) {
383,325✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
381,309✔
545
  }
546

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

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

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

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

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

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

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

638
  return code;
383,325✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
477,179,319✔
647
    return TSDB_CODE_INVALID_PARA;
13,207✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
477,235,288✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
477,235,288✔
652
    return TSDB_CODE_SUCCESS;
281,050,807✔
653
  }
654

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

661
  *sversion = pSchemaInfo->sw->version;
196,265,156✔
662
  *tversion = pSchemaInfo->tversion;
196,206,857✔
663
  *rversion = pSchemaInfo->rversion;
196,291,989✔
664
  if (pSchemaInfo->dbname) {
196,143,969✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
196,144,945✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
196,314,271✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
196,313,016✔
671
  } else {
672
    tableName[0] = 0;
12✔
673
  }
674

675
  *tbGet = true;
196,235,817✔
676

677
  return TSDB_CODE_SUCCESS;
196,165,883✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
280,858,383✔
681

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

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

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

714
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
20,854,798✔
715
  int32_t        code = TSDB_CODE_SUCCESS;
20,854,798✔
716
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
20,854,798✔
717
  if (pTask->pWorkerCb) {
20,854,798✔
718
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
20,866,637✔
719
    if (code != TSDB_CODE_SUCCESS) {
20,893,324✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724

725
  code = tsem_wait(pSem);
20,882,007✔
726
  if (code != TSDB_CODE_SUCCESS) {
20,893,831✔
727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
728
    pTask->code = code;
×
729
    return pTask->code;
×
730
  }
731

732
  if (pTask->pWorkerCb) {
20,893,843✔
733
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
20,893,843✔
734
    if (code != TSDB_CODE_SUCCESS) {
20,895,798✔
735
      pTask->code = code;
×
736
      return pTask->code;
×
737
    }
738
  }
739
  return TSDB_CODE_SUCCESS;
20,895,798✔
740
}
741

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

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

751
  readHandle->uid = 0;
286,505,807✔
752
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
286,557,635✔
753
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
286,088,136✔
754
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
3,484,148✔
755
    goto _error;
3,479,827✔
756
  }
757
    
758
  if (handle) {
282,723,505✔
759
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
282,366,432✔
760
    void*           pSinkManager = NULL;
282,477,476✔
761
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
282,350,809✔
762
    if (code != TSDB_CODE_SUCCESS) {
281,890,782✔
763
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
764
      goto _error;
×
765
    }
766

767
    void* pSinkParam = NULL;
281,890,782✔
768
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
282,141,273✔
769
    if (code != TSDB_CODE_SUCCESS) {
281,700,101✔
770
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
771
      taosMemoryFree(pSinkManager);
×
772
      goto _error;
×
773
    }
774

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

786
    // pSinkParam has been freed during create sinker.
787
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
281,746,796✔
788
                              (*pTask)->id.str, pSubplan->processOneBlock);
282,413,809✔
789
    if (code) {
281,771,418✔
790
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
583✔
791
    }
792
  }
793

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

797
_error:
91,387,736✔
798
  // if failed to add ref for all tables in this query, abort current query
799
  return code;
286,282,675✔
800
}
801

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

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

814
  if (pLocal) {
328,944,266✔
815
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
325,548,477✔
816
  }
817

818
  taosArrayClear(pResList);
328,921,070✔
819

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

827
  if (pTaskInfo->cost.start == 0) {
328,903,206✔
828
    pTaskInfo->cost.start = taosGetTimestampUs();
278,228,536✔
829
  }
830

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

837
  // error occurs, record the error code and return to client
838
  int32_t ret = setjmp(pTaskInfo->env);
328,911,618✔
839
  if (ret != TSDB_CODE_SUCCESS) {
332,170,508✔
840
    pTaskInfo->code = ret;
3,514,979✔
841
    (void)cleanUpUdfs();
3,514,979✔
842

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

846
    return pTaskInfo->code;
3,514,979✔
847
  }
848

849
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
328,655,529✔
850

851
  int32_t      current = 0;
328,658,366✔
852
  SSDataBlock* pRes = NULL;
328,658,366✔
853
  int64_t      st = taosGetTimestampUs();
328,935,168✔
854

855
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
328,935,168✔
856
    pTaskInfo->paramSet = true;
31,710,287✔
857
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
31,709,481✔
858
  } else {
859
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
297,277,593✔
860
  }
861

862
  QUERY_CHECK_CODE(code, lino, _end);
325,588,703✔
863
  code = blockDataCheck(pRes);
325,588,703✔
864
  QUERY_CHECK_CODE(code, lino, _end);
325,631,607✔
865

866
  if (pRes == NULL) {
325,631,607✔
867
    st = taosGetTimestampUs();
63,817,807✔
868
  }
869

870
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
325,624,251✔
871
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
325,661,833✔
872
    rowsThreshold = 4096;
325,182,889✔
873
  }
874

875
  int32_t blockIndex = 0;
325,652,421✔
876
  while (pRes != NULL) {
821,137,152✔
877
    SSDataBlock* p = NULL;
538,199,187✔
878
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
538,170,234✔
879
      SSDataBlock* p1 = NULL;
424,679,449✔
880
      code = createOneDataBlock(pRes, true, &p1);
424,684,014✔
881
      QUERY_CHECK_CODE(code, lino, _end);
424,729,161✔
882

883
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
424,729,161✔
884
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
424,753,215✔
885
      p = p1;
424,753,215✔
886
    } else {
887
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
113,525,782✔
888
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
113,526,578✔
889

890
      p = *(SSDataBlock**)tmp;
113,526,578✔
891
      code = copyDataBlock(p, pRes);
113,526,578✔
892
      QUERY_CHECK_CODE(code, lino, _end);
113,526,793✔
893
    }
894

895
    blockIndex += 1;
538,263,689✔
896

897
    current += p->info.rows;
538,263,689✔
898
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
538,270,718✔
899
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
900
    void* tmp = taosArrayPush(pResList, &p);
538,229,507✔
901
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
538,229,507✔
902

903
    if (current >= rowsThreshold || processOneBlock) {
538,229,507✔
904
      break;
905
    }
906

907
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
495,497,581✔
908
    QUERY_CHECK_CODE(code, lino, _end);
495,493,726✔
909
    code = blockDataCheck(pRes);
495,493,726✔
910
    QUERY_CHECK_CODE(code, lino, _end);
495,516,260✔
911
  }
912

913
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
325,669,891✔
914
    pTaskInfo->pSubplan->rowsThreshold -= current;
448,240✔
915
  }
916

917
  *hasMore = (pRes != NULL);
325,658,367✔
918
  uint64_t el = (taosGetTimestampUs() - st);
325,598,309✔
919

920
  pTaskInfo->cost.elapsedTime += el;
325,598,309✔
921
  if (NULL == pRes) {
325,633,638✔
922
    *useconds = pTaskInfo->cost.elapsedTime;
282,881,110✔
923
  }
924

925
_end:
284,791,367✔
926
  (void)cleanUpUdfs();
325,668,027✔
927

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

932
  atomic_store_64(&pTaskInfo->owner, 0);
325,751,886✔
933
  if (code) {
325,750,649✔
934
    pTaskInfo->code = code;
×
935
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
936
  }
937

938
  return pTaskInfo->code;
325,750,963✔
939
}
940

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

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

955
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
125,505,711✔
956
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
125,505,711✔
957
  int64_t        threadId = taosGetSelfPthreadId();
125,505,711✔
958
  int64_t        curOwner = 0;
125,509,405✔
959

960
  *pRes = NULL;
125,509,405✔
961

962
  // todo extract method
963
  taosRLockLatch(&pTaskInfo->lock);
125,509,702✔
964
  bool isKilled = isTaskKilled(pTaskInfo);
125,509,373✔
965
  if (isKilled) {
125,510,059✔
966
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
967

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

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

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

980
  pTaskInfo->owner = threadId;
125,509,359✔
981
  taosRUnLockLatch(&pTaskInfo->lock);
125,509,419✔
982

983
  if (pTaskInfo->cost.start == 0) {
125,510,063✔
984
    pTaskInfo->cost.start = taosGetTimestampUs();
230,706✔
985
  }
986

987
  // error occurs, record the error code and return to client
988
  int32_t ret = setjmp(pTaskInfo->env);
125,510,402✔
989
  if (ret != TSDB_CODE_SUCCESS) {
125,504,612✔
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));
×
993
    atomic_store_64(&pTaskInfo->owner, 0);
×
994
    return pTaskInfo->code;
×
995
  }
996

997
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
125,504,612✔
998

999
  int64_t st = taosGetTimestampUs();
125,505,630✔
1000
  int32_t code = TSDB_CODE_SUCCESS;
125,505,630✔
1001
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
125,505,630✔
1002
    pTaskInfo->paramSet = true;
×
1003
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1004
  } else {
1005
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
125,507,002✔
1006
  }
1007
  if (code) {
125,460,443✔
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);
125,460,443✔
1013
  if (code) {
125,502,454✔
1014
    pTaskInfo->code = code;
×
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);
125,485,137✔
1019

1020
  pTaskInfo->cost.elapsedTime += el;
125,485,137✔
1021
  if (NULL == *pRes) {
125,487,729✔
1022
    *useconds = pTaskInfo->cost.elapsedTime;
21,199,092✔
1023
  }
1024

1025
  (void)cleanUpUdfs();
125,490,929✔
1026

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

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

1033
  atomic_store_64(&pTaskInfo->owner, 0);
125,510,063✔
1034
  return pTaskInfo->code;
125,509,377✔
1035
}
1036

1037
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
98,853,251✔
1038
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
98,853,251✔
1039
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
98,858,536✔
1040
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
98,858,530✔
1041

1042
  if (!tmp) {
98,854,160✔
1043
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
782✔
1044
    return terrno;
782✔
1045
  }
1046
  return TSDB_CODE_SUCCESS;
98,853,378✔
1047
}
1048

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

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

1059
  return 0;
×
1060
}
1061

1062
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
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) {
156,076✔
1072
  if (pTaskInfo->subJobCtx.hasSubJobs) {
156,076✔
1073
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
121,271✔
1074
    if (pTaskInfo->subJobCtx.param) {
121,271✔
1075
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
68,522✔
1076
    }
1077
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
121,271✔
1078
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
121,271✔
1079
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
121,271✔
1080
  }
1081
  
1082
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
156,076✔
1083

1084
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
156,076✔
1085
  for (int32_t i = 0; i < num; ++i) {
169,584✔
1086
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
13,508✔
1087
    if (!pStop) {
13,508✔
1088
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1089
      continue;
×
1090
    }
1091
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
13,508✔
1092
    if (pExchangeInfo) {
13,508✔
1093
      int32_t code = tsem_post(&pExchangeInfo->ready);
13,508✔
1094
      if (code != TSDB_CODE_SUCCESS) {
13,508✔
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);
13,508✔
1098
      }
1099
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
13,508✔
1100
      if (code != TSDB_CODE_SUCCESS) {
13,508✔
1101
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1102
      }
1103
    }
1104
  }
1105

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

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

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

1117
  setTaskKilled(pTaskInfo, rspCode);
156,076✔
1118
  qStopTaskOperators(pTaskInfo);
156,076✔
1119

1120
  return TSDB_CODE_SUCCESS;
156,076✔
1121
}
1122

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

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

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

1144
        taosMsleep(200);
×
1145

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

1159
  int64_t et = taosGetTimestampMs() - st;
×
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) {
×
1170
    return false;
×
1171
  }
1172

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

1176
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
283,752,991✔
1177
  STaskCostInfo* pSummary = &pTaskInfo->cost;
283,752,991✔
1178
  int64_t        idleTime = pSummary->start - pSummary->created;
283,770,255✔
1179

1180
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
283,763,501✔
1181
  if (pSummary->pRecoder != NULL) {
283,737,917✔
1182
    qDebug(
194,929,438✔
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,
88,783,452✔
1191
           pSummary->elapsedTime / 1000.0);
1192
  }
1193
}
283,712,508✔
1194

1195

1196
void qDestroyTask(qTaskInfo_t qTaskHandle) {
292,793,740✔
1197
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
292,793,740✔
1198
  if (pTaskInfo == NULL) {
292,793,740✔
1199
    return;
9,037,655✔
1200
  }
1201

1202
  if (pTaskInfo->pRoot != NULL) {
283,756,085✔
1203
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
283,797,474✔
1204
  } else {
1205
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1206
  }
1207

1208
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
283,771,297✔
1209
  doDestroyTask(pTaskInfo);
283,739,653✔
1210
}
1211

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

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

1221
  while (1) {
318,377✔
1222
    uint16_t type = pOperator->operatorType;
642,734✔
1223
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
642,461✔
1224
      *scanner = pOperator->info;
324,084✔
1225
      break;
324,357✔
1226
    } else {
1227
      pOperator = pOperator->pDownstream[0];
318,377✔
1228
    }
1229
  }
1230
}
324,357✔
1231

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

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

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

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

1252
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
22,272,310✔
1253
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22,272,310✔
1254
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
22,272,310✔
1255
  return 0;
22,272,310✔
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) {
770,085✔
1264
  memset(pCond, 0, sizeof(SQueryTableDataCond));
770,085✔
1265
  pCond->order = TSDB_ORDER_ASC;
770,085✔
1266
  pCond->numOfCols = pMtInfo->schema->nCols;
770,699✔
1267
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
771,590✔
1268
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
770,976✔
1269
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
771,293✔
1270
    taosMemoryFreeClear(pCond->colList);
604✔
1271
    taosMemoryFreeClear(pCond->pSlotList);
×
1272
    return terrno;
×
1273
  }
1274

1275
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
770,689✔
1276
  pCond->suid = pMtInfo->suid;
770,689✔
1277
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
770,976✔
1278
  pCond->startVersion = -1;
770,976✔
1279
  pCond->endVersion = sContext->snapVersion;
770,976✔
1280

1281
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,910,946✔
1282
    SColumnInfo* pColInfo = &pCond->colList[i];
4,139,366✔
1283
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,139,059✔
1284
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,139,970✔
1285
    if (pMtInfo->pExtSchemas != NULL) {
4,139,673✔
1286
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,000✔
1287
    }
1288
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,140,267✔
1289
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,139,663✔
1290

1291
    pCond->pSlotList[i] = i;
4,139,980✔
1292
  }
1293

1294
  return TSDB_CODE_SUCCESS;
771,590✔
1295
}
1296

1297
void qStreamSetOpen(qTaskInfo_t tinfo) {
123,976,341✔
1298
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
123,976,341✔
1299
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
123,976,341✔
1300
  pOperator->status = OP_NOT_OPENED;
123,989,667✔
1301
}
123,989,363✔
1302

1303
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
21,443,597✔
1304
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
21,443,597✔
1305
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
21,443,597✔
1306
}
21,443,253✔
1307

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

1313
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22,443,841✔
1314
  const char*    id = GET_TASKID(pTaskInfo);
22,442,883✔
1315

1316
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22,440,657✔
1317
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
21,148,455✔
1318
    if (pOperator == NULL || code != 0) {
21,147,772✔
1319
      return code;
×
1320
    }
1321

1322
    SStreamScanInfo* pInfo = pOperator->info;
21,147,845✔
1323
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
21,148,116✔
1324
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
21,146,773✔
1325
    walReaderVerifyOffset(pWalReader, pOffset);
21,146,861✔
1326
  }
1327
  // if pOffset equal to current offset, means continue consume
1328
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
22,440,310✔
1329
    return 0;
20,559,993✔
1330
  }
1331

1332
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,877,332✔
1333
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,089,613✔
1334
    if (pOperator == NULL || code != 0) {
1,089,937✔
1335
      return code;
×
1336
    }
1337

1338
    SStreamScanInfo* pInfo = pOperator->info;
1,089,937✔
1339
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,089,937✔
1340
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,089,651✔
1341
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,090,525✔
1342

1343
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,089,594✔
1344
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
881,150✔
1345
      pScanBaseInfo->dataReader = NULL;
880,823✔
1346

1347
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
880,823✔
1348
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
880,823✔
1349
      walReaderVerifyOffset(pWalReader, pOffset);
881,150✔
1350
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
881,150✔
1351
      if (code < 0) {
881,150✔
1352
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
12,248✔
1353
        return code;
12,248✔
1354
      }
1355
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
208,775✔
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,375✔
1359
      int64_t ts = pOffset->ts;
209,375✔
1360
      int32_t index = 0;
209,375✔
1361

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

1372
      if (uid == 0) {
209,375✔
1373
        if (numOfTables != 0) {
201,937✔
1374
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,882✔
1375
          if (!tmp) {
38,882✔
1376
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1377
            taosRUnLockLatch(&pTaskInfo->lock);
×
1378
            return terrno;
×
1379
          }
1380
          if (tmp) uid = tmp->uid;
38,882✔
1381
          ts = INT64_MIN;
39,192✔
1382
          pScanInfo->currentTable = 0;
39,192✔
1383
        } else {
1384
          taosRUnLockLatch(&pTaskInfo->lock);
163,055✔
1385
          qError("no table in table list, %s", id);
163,027✔
1386
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
163,341✔
1387
        }
1388
      }
1389
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
46,010✔
1390

1391
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
45,727✔
1392
             pInfo->pTableScanOp->resultInfo.totalRows);
1393
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
46,034✔
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,034✔
1399
      taosRUnLockLatch(&pTaskInfo->lock);
46,034✔
1400

1401
      if (index >= 0) {
46,034✔
1402
        pScanInfo->currentTable = index;
46,034✔
1403
      } else {
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);
1406
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1407
      }
1408

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

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

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

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

1438
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,468✔
1439
        if (code != TSDB_CODE_SUCCESS) {
6,468✔
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,468✔
1444
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1445
      }
1446

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

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

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

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

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

1472
      SMetaTableInfo mtInfo = {0};
772,128✔
1473
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
772,128✔
1474
      if (code != 0) {
772,128✔
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
772,128✔
1479
      pInfo->dataReader = NULL;
770,623✔
1480

1481
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
770,623✔
1482
      tableListClear(pTableListInfo);
770,603✔
1483

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

1489
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
770,402✔
1490
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
770,986✔
1491
      if (code != TSDB_CODE_SUCCESS) {
770,689✔
1492
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1493
        destroyMetaTableInfo(&mtInfo);
1494
        return code;
×
1495
      }
1496
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
770,689✔
1497
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,303✔
1498
      } else {
1499
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
769,673✔
1500
      }
1501

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

1509
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
771,590✔
1510
      if (!pList) {
771,590✔
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
1513
        return code;
×
1514
      }
1515
      int32_t size = 0;
771,590✔
1516
      code = tableListGetSize(pTableListInfo, &size);
771,590✔
1517
      if (code != TSDB_CODE_SUCCESS) {
771,590✔
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,543,180✔
1524
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
771,590✔
1525
      if (code != TSDB_CODE_SUCCESS) {
770,996✔
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);
770,996✔
1532
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
771,293✔
1533
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1534
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
771,293✔
1535
      pTaskInfo->streamInfo.schema = mtInfo.schema;
770,996✔
1536
      taosMemoryFreeClear(mtInfo.pExtSchemas);
770,382✔
1537

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

1557
end:
1,702,539✔
1558
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,702,348✔
1559
  return 0;
1,702,655✔
1560
}
1561

1562
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
231,421,056✔
1563
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
231,421,056✔
1564
  if (pMsg->info.ahandle == NULL) {
231,448,259✔
1565
    rpcFreeCont(pMsg->pCont);
564✔
1566
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
564✔
1567
    return;
564✔
1568
  }
1569

1570
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
231,365,880✔
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};
231,386,037✔
1574

1575
  if (pMsg->contLen > 0) {
231,417,428✔
1576
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
231,412,224✔
1577
    if (buf.pData == NULL) {
231,354,690✔
1578
      pMsg->code = terrno;
×
1579
    } else {
1580
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
231,354,690✔
1581
    }
1582
  }
1583

1584
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
231,366,511✔
1585
  rpcFreeCont(pMsg->pCont);
231,533,897✔
1586
  destroySendMsgInfo(pSendInfo);
231,457,745✔
1587
}
1588

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

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

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

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);
×
1613
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1614
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1615
  }
1616

1617
  taosArrayDestroy(plist);
×
1618

1619
_end:
×
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
  }
1624
  return pUidList;
×
1625
}
1626

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

1632
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,344,532✔
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,344,532✔
1639
    STableScanInfo* pScanInfo = pOperator->info;
1,672,266✔
1640

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

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

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

1661
  *pList = NULL;
1,672,266✔
1662
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,672,266✔
1663
  if (pArray == NULL) {
1,671,191✔
1664
    return terrno;
×
1665
  }
1666

1667
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,671,191✔
1668
  if (code == 0) {
1,671,191✔
1669
    *pList = pArray;
1,671,191✔
1670
  } else {
1671
    taosArrayDestroy(pArray);
×
1672
  }
1673
  return code;
1,671,191✔
1674
}
1675

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

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

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

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

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

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

1711
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
27,960,142✔
1712
  int32_t code = 0;
27,960,142✔
1713

1714
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
27,960,142✔
1715
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
27,961,233✔
1716

1717
  if (pOper->fpSet.resetStateFn) {
27,960,804✔
1718
    code = pOper->fpSet.resetStateFn(pOper);
27,961,485✔
1719
  }
1720
  pOper->status = OP_NOT_OPENED;
27,958,231✔
1721
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
48,026,690✔
1722
    code = clearStatesForOperator(pOper->pDownstream[i]);
20,064,096✔
1723
  }
1724
  return code;
27,963,806✔
1725
}
1726

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

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

1741
  *ppRes = NULL;
10,708,678✔
1742

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

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

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

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

1762
  pTaskInfo->owner = threadId;
10,708,415✔
1763
  taosRUnLockLatch(&pTaskInfo->lock);
10,708,659✔
1764

1765
  if (pTaskInfo->cost.start == 0) {
10,708,443✔
1766
    pTaskInfo->cost.start = taosGetTimestampUs();
226,831✔
1767
  }
1768

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

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

1781
  int64_t st = taosGetTimestampUs();
10,708,894✔
1782

1783
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,708,894✔
1784
  if (code) {
8,740,318✔
1785
    pTaskInfo->code = code;
×
1786
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1787
  } else {
1788
    *finished = *ppRes == NULL;
8,740,318✔
1789
    code = blockDataCheck(*ppRes);
8,740,318✔
1790
  }
1791
  if (code) {
8,740,318✔
1792
    pTaskInfo->code = code;
×
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);
8,740,102✔
1797

1798
  pTaskInfo->cost.elapsedTime += el;
8,740,102✔
1799
  if (NULL == *ppRes) {
8,740,105✔
1800
    *useconds = pTaskInfo->cost.elapsedTime;
5,493,627✔
1801
  }
1802

1803
  (void)cleanUpUdfs();
8,740,102✔
1804

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

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

1811
  atomic_store_64(&pTaskInfo->owner, 0);
8,740,318✔
1812
  return pTaskInfo->code;
8,740,318✔
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,
236,172✔
1821
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1822
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1823
  int32_t code = 0;                                        
236,172✔
1824
  if (*pTableListInfo != NULL) {
236,172✔
1825
    qDebug("table list already exists, no need to create again");
×
1826
    goto end;
×
1827
  }
1828
  STableListInfo* pList = tableListCreate();
236,172✔
1829
  if (pList == NULL) {
236,172✔
1830
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1831
    code = terrno;
×
1832
    goto end;
×
1833
  }
1834

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

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

1847
end:
236,172✔
1848
  return 0;
236,172✔
1849
}
1850

1851
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
37,810✔
1852
  bool   listAdded = false;
37,810✔
1853
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
37,810✔
1854
  if (code == 0 && !listAdded) {
37,810✔
1855
    int32_t numOfTables = taosArrayGetSize(pUidList);
31,846✔
1856
    for (int i = 0; i < numOfTables; i++) {
92,816✔
1857
      void* tmp = taosArrayGet(pUidList, i);
60,970✔
1858
      if (tmp == NULL) {
60,970✔
1859
        return terrno;
×
1860
      }
1861
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
60,970✔
1862

1863
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
60,970✔
1864
      if (p == NULL) {
60,970✔
1865
        return terrno;
×
1866
      }
1867
    }
1868
  }
1869
  return code;
37,810✔
1870
}
1871

1872
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
30,122✔
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,122✔
1876
  STableListInfo* pList = tableListCreate();
30,122✔
1877
  if (pList == NULL) {
30,122✔
1878
    code = terrno;
×
1879
    goto end;
×
1880
  }
1881
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
30,122✔
1882
  if (uidListCopy == NULL) {
30,122✔
1883
    code = terrno;
×
1884
    goto end;
×
1885
  }
1886
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
30,122✔
1887
  SReadHandle    pHandle = {.vnode = pVnode};
30,122✔
1888

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

1902
  taosArrayClear(uidList);
30,122✔
1903
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
60,244✔
1904
    void* tmp = taosArrayGet(uidListCopy, i);
30,122✔
1905
    if (tmp == NULL) {
30,122✔
1906
      continue;
×
1907
    }
1908
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
30,122✔
1909
    if (slot == NULL) {
30,122✔
1910
      if (taosArrayPush(uidList, tmp) == NULL) {
3,339✔
1911
        code = terrno;
×
1912
        goto end;
×
1913
      }
1914
    }
1915
  }
1916
end:
30,122✔
1917
  taosArrayDestroy(uidListCopy);
30,122✔
1918
  tableListDestroy(pList);
30,122✔
1919
  return code;
30,122✔
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); }
236,172✔
1944
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
236,172✔
1945
  STableListInfo* pList = pTableListInfo;
236,172✔
1946
  return pList->pTableList;
236,172✔
1947
}
1948

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

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

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

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

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

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

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

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

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

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

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

2124
      pCol->hasNull = true;
236,316✔
2125
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
236,316✔
2126
      pCol->info.colId = 0;
236,316✔
2127
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
236,316✔
2128
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
236,316✔
2129
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
236,316✔
2130
      code = colInfoDataEnsureCapacity(pCol, 1, true);
236,316✔
2131
      if (code != 0) {
236,316✔
2132
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2133
        taosMemoryFree(pCol);
×
2134
        break;
×
2135
      }
2136
      dst.columnData = pCol;
236,316✔
2137
      dst.numOfRows = 1;
236,316✔
2138
      dst.colAlloced = true;
236,316✔
2139
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
236,316✔
2140
      if (colDataIsNull_var(dst.columnData, 0)) {
236,316✔
2141
        qInfo("invalid sub tb expr with null value");
2,310✔
2142
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,475✔
2143
      }
2144
      if (code == 0) {
236,529✔
2145
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
233,841✔
2146
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
233,226✔
2147
      }
2148
    } break;
236,103✔
2149
    default:
×
2150
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2151
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2152
      break;
×
2153
  }
2154
  if (code == 0) {
236,103✔
2155
    if (!pVal || len == 0) {
233,226✔
2156
      qError("tbname generated with no characters which is not allowed");
×
2157
      code = TSDB_CODE_INVALID_PARA;
×
2158
    }
2159
    if(len > TSDB_TABLE_NAME_LEN - 1) {
233,628✔
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);
233,628✔
2165
    tbname[len] = '\0';  // ensure null terminated
233,628✔
2166
    if (NULL != strchr(tbname, '.')) {
232,764✔
2167
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2168
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2169
    }
2170
  }
2171
  // TODO free dst
2172
  sclFreeParam(&dst);
235,641✔
2173
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
235,914✔
2174
  return code;
235,914✔
2175
}
2176

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

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

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

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

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

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

2235
  if (pSrc->pFields && pSrc->pFields->size > 0) {
220,071✔
2236
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
220,290✔
2237
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
220,290✔
2238
  } else {
2239
    (*ppDst)->pFields = NULL;
×
2240
  }
2241
  
2242
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
220,290✔
2243
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
130,674✔
2244
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
130,674✔
2245
  } else {
2246
    (*ppDst)->pTagFields = NULL;
89,616✔
2247
  }
2248

2249
  if (pSrc->colCids && pSrc->colCids->size > 0) {
220,290✔
2250
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,736✔
2251
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,736✔
2252
  } else {
2253
    (*ppDst)->colCids = NULL;
218,554✔
2254
  }
2255

2256
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
220,290✔
2257
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,106✔
2258
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,106✔
2259
  } else {
2260
    (*ppDst)->tagCids = NULL;
219,184✔
2261
  }
2262

2263
_exit:
220,290✔
2264

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

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

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) {
7,688✔
2285
  int32_t         code = TSDB_CODE_SUCCESS;
7,688✔
2286
  STableListInfo* pList = tableListCreate();
7,688✔
2287
  if (pList == NULL) {
7,688✔
2288
    code = terrno;
×
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;
7,688✔
2298
  pList->idInfo.suid = suid;
7,688✔
2299
  pList->idInfo.tableType = TD_SUPER_TABLE;
7,688✔
2300
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
7,688✔
2301
  if (code != TSDB_CODE_SUCCESS) {
7,688✔
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:
7,688✔
2319
  // taosArrayDestroy(uidListCopy);
2320
  tableListDestroy(pList);
7,688✔
2321
  return code;
7,688✔
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