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

taosdata / TDengine / #4925

12 Jan 2026 09:34AM UTC coverage: 66.107% (+0.8%) from 65.354%
#4925

push

travis-ci

web-flow
merge: from main to 3.0 branch #34248

103 of 129 new or added lines in 9 files covered. (79.84%)

891 existing lines in 139 files now uncovered.

200488 of 303278 relevant lines covered (66.11%)

129810096.48 hits per line

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

69.29
/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) {
895,951,996✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
895,951,996✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
895,951,996✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
896,080,330✔
47
}
895,989,774✔
48

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

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

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

70
static void initRefPool() {
540,153✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
540,153✔
72
  (void)atexit(cleanupRefPool);
540,153✔
73
}
540,153✔
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) {
41,825,747✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
41,825,747✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
41,826,294✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
18,891,178✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
18,891,496✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
18,891,218✔
162
      if (pScanInfo->base.dataReader != NULL) {
18,891,218✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
262,094✔
164
        if (code) {
262,094✔
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);
22,933,307✔
172
  }
173

174
  return 0;
22,450,981✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
22,452,255✔
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) {
427,842✔
213
  if (msg == NULL) {  // create raw scan
427,842✔
214
    SExecTaskInfo* pTaskInfo = NULL;
115,237✔
215

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

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

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

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

248
  return pTaskInfo;
316,046✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
499,985✔
252
  if (streamInserterParam == NULL) {
499,985✔
253
    return TSDB_CODE_SUCCESS;
295,332✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
204,653✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
204,864✔
323
    if (code) {
204,653✔
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,
499,555✔
328
         tstrerror(code));
329

330
_error:
74,162✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
3,060,550✔
342
  if (pInfo == NULL) {
3,060,550✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,060,550✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,060,550✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,060,550✔
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
3,060,550✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,809,542✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
731,678✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,870,092✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

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

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,060,550✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
251,008✔
382
    setReadHandle(handle, pScanBaseInfo);
251,008✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,809,542✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,077,864✔
385
    setReadHandle(handle, pScanBaseInfo);
2,077,864✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
500,196✔
400

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
367,349✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
367,349✔
444

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

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

454
  locked = 1;
367,349✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
748,918✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
381,569✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
381,569✔
458

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

465
    tDecoderClear(&mr.coder);
381,569✔
466

467
    if (mr.me.type == TSDB_SUPER_TABLE) {
381,569✔
468
      continue;
×
469
    } else {
470
      if (type == TSDB_SUPER_TABLE) {
381,569✔
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) {
381,569✔
473
          continue;
3,460✔
474
        }
475
      } else {  // ordinary table
476
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
477
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
478
        if (mr.me.uid != uid) {
×
479
          continue;
×
480
        }
481
      }
482
    }
483

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
367,349✔
524

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

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

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

543
  if (isAdd) {
369,370✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
367,349✔
545
  }
546

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

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

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

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

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

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

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

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

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

638
  return code;
369,370✔
639
}
640

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

646
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
692,064,533✔
647
    return TSDB_CODE_INVALID_PARA;
77,182✔
648
  }
649
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
691,987,441✔
650

651
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
691,987,441✔
652
    return TSDB_CODE_SUCCESS;
396,477,933✔
653
  }
654

655
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
295,487,890✔
656
  if (!pSchemaInfo) {
294,691,182✔
657
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
658
    return terrno;
×
659
  }
660

661
  *sversion = pSchemaInfo->sw->version;
294,691,182✔
662
  *tversion = pSchemaInfo->tversion;
295,865,901✔
663
  *rversion = pSchemaInfo->rversion;
295,586,844✔
664
  if (pSchemaInfo->dbname) {
296,010,435✔
665
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
296,003,109✔
666
  } else {
667
    dbName[0] = 0;
×
668
  }
669
  if (pSchemaInfo->tablename) {
296,442,323✔
670
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
296,168,047✔
671
  } else {
672
    tableName[0] = 0;
1,403✔
673
  }
674

675
  *tbGet = true;
296,420,411✔
676

677
  return TSDB_CODE_SUCCESS;
296,044,658✔
678
}
679

680
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
396,103,943✔
681

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

689
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
76,404,975✔
690
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
76,404,975✔
691
  ((SExecTaskInfo*)tinfo)->paramSet = false;
76,409,418✔
692
}
76,410,074✔
693

694
int32_t qExecutorInit(void) {
4,218,545✔
695
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,218,545✔
696
  return TSDB_CODE_SUCCESS;
4,220,187✔
697
}
698

699
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
62,904,574✔
700
  int32_t        code = TSDB_CODE_SUCCESS;
62,904,574✔
701
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
62,904,574✔
702
  if (pTask->pWorkerCb) {
62,904,574✔
703
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
62,887,806✔
704
    if (code != TSDB_CODE_SUCCESS) {
62,940,431✔
705
      pTask->code = code;
×
706
      return pTask->code;
×
707
    }
708
  }
709

710
  code = tsem_wait(pSem);
62,986,097✔
711
  if (code != TSDB_CODE_SUCCESS) {
62,943,565✔
712
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
713
    pTask->code = code;
×
714
    return pTask->code;
×
715
  }
716

717
  if (pTask->pWorkerCb) {
62,943,565✔
718
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
62,945,295✔
719
    if (code != TSDB_CODE_SUCCESS) {
62,945,261✔
720
      pTask->code = code;
×
721
      return pTask->code;
×
722
    }
723
  }
724
  return TSDB_CODE_SUCCESS;
62,944,413✔
725
}
726

727
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
407,846,188✔
728
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
729
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
730
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
407,846,188✔
731
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
407,846,188✔
732

733
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
407,768,184✔
734
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
735

736
  readHandle->uid = 0;
407,807,539✔
737
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
407,895,742✔
738
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
407,290,240✔
739
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
9,441,988✔
740
    goto _error;
9,505,933✔
741
  }
742
    
743
  if (handle) {
397,951,206✔
744
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
397,706,628✔
745
    void*           pSinkManager = NULL;
397,900,245✔
746
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
397,646,024✔
747
    if (code != TSDB_CODE_SUCCESS) {
396,618,150✔
748
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
749
      goto _error;
×
750
    }
751

752
    void* pSinkParam = NULL;
396,618,150✔
753
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
397,205,721✔
754
    if (code != TSDB_CODE_SUCCESS) {
396,782,833✔
755
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
756
      taosMemoryFree(pSinkManager);
×
757
      goto _error;
×
758
    }
759

760
    SDataSinkNode* pSink = NULL;
396,782,833✔
761
    if (readHandle->localExec) {
397,309,270✔
762
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,860✔
763
      if (code != TSDB_CODE_SUCCESS) {
2,860✔
764
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
765
               (*pTask)->id.str);
766
        taosMemoryFree(pSinkManager);
×
767
        goto _error;
×
768
      }
769
    }
770

771
    // pSinkParam has been freed during create sinker.
772
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
396,692,363✔
773
                              (*pTask)->id.str, pSubplan->processOneBlock);
397,546,229✔
774
    if (code) {
396,586,102✔
775
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
583✔
776
    }
777
  }
778

779
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
397,421,026✔
780
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
781

782
_error:
187,238,305✔
783
  // if failed to add ref for all tables in this query, abort current query
784
  return code;
407,046,693✔
785
}
786

787
static void freeBlock(void* param) {
×
788
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
789
  blockDataDestroy(pBlock);
×
790
}
×
791

792
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
491,289,310✔
793
                     bool processOneBlock) {
794
  int32_t        code = TSDB_CODE_SUCCESS;
491,289,310✔
795
  int32_t        lino = 0;
491,289,310✔
796
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
491,289,310✔
797
  int64_t        threadId = taosGetSelfPthreadId();
491,289,310✔
798

799
  if (pLocal) {
491,041,070✔
800
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
487,762,993✔
801
  }
802

803
  taosArrayClear(pResList);
491,021,822✔
804

805
  int64_t curOwner = 0;
490,847,865✔
806
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
490,847,865✔
807
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
808
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
809
    return pTaskInfo->code;
×
810
  }
811

812
  if (pTaskInfo->cost.start == 0) {
490,586,757✔
813
    pTaskInfo->cost.start = taosGetTimestampUs();
392,251,324✔
814
  }
815

816
  if (isTaskKilled(pTaskInfo)) {
491,469,570✔
817
    atomic_store_64(&pTaskInfo->owner, 0);
294✔
818
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
294✔
819
    return pTaskInfo->code;
294✔
820
  }
821

822
  // error occurs, record the error code and return to client
823
  int32_t ret = setjmp(pTaskInfo->env);
490,690,148✔
824
  if (ret != TSDB_CODE_SUCCESS) {
498,936,259✔
825
    pTaskInfo->code = ret;
8,984,588✔
826
    (void)cleanUpUdfs();
8,984,882✔
827

828
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
8,985,764✔
829
    atomic_store_64(&pTaskInfo->owner, 0);
8,985,764✔
830

831
    return pTaskInfo->code;
8,985,764✔
832
  }
833

834
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
489,951,671✔
835

836
  int32_t      current = 0;
489,952,829✔
837
  SSDataBlock* pRes = NULL;
489,952,829✔
838
  int64_t      st = taosGetTimestampUs();
491,090,923✔
839

840
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
491,090,923✔
841
    pTaskInfo->paramSet = true;
76,399,715✔
842
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
76,402,840✔
843
  } else {
844
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
414,848,627✔
845
  }
846

847
  QUERY_CHECK_CODE(code, lino, _end);
482,557,031✔
848
  code = blockDataCheck(pRes);
482,557,031✔
849
  QUERY_CHECK_CODE(code, lino, _end);
482,630,522✔
850

851
  if (pRes == NULL) {
482,630,522✔
852
    st = taosGetTimestampUs();
103,329,323✔
853
  }
854

855
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
482,630,007✔
856
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
482,656,339✔
857
    rowsThreshold = 4096;
482,108,903✔
858
  }
859

860
  int32_t blockIndex = 0;
482,652,312✔
861
  while (pRes != NULL) {
1,100,613,477✔
862
    SSDataBlock* p = NULL;
666,413,592✔
863
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
666,256,060✔
864
      SSDataBlock* p1 = NULL;
509,650,230✔
865
      code = createOneDataBlock(pRes, true, &p1);
509,673,744✔
866
      QUERY_CHECK_CODE(code, lino, _end);
509,572,913✔
867

868
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
509,572,913✔
869
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
509,726,890✔
870
      p = p1;
509,726,890✔
871
    } else {
872
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
156,742,320✔
873
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
156,741,876✔
874

875
      p = *(SSDataBlock**)tmp;
156,741,876✔
876
      code = copyDataBlock(p, pRes);
156,741,876✔
877
      QUERY_CHECK_CODE(code, lino, _end);
156,741,411✔
878
    }
879

880
    blockIndex += 1;
666,434,861✔
881

882
    current += p->info.rows;
666,434,861✔
883
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
666,483,125✔
884
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
885
    void* tmp = taosArrayPush(pResList, &p);
666,466,358✔
886
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
666,466,358✔
887

888
    if (current >= rowsThreshold || processOneBlock) {
666,466,358✔
889
      break;
890
    }
891

892
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
617,975,010✔
893
    QUERY_CHECK_CODE(code, lino, _end);
617,984,127✔
894
    code = blockDataCheck(pRes);
617,984,127✔
895
    QUERY_CHECK_CODE(code, lino, _end);
618,009,697✔
896
  }
897

898
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
482,691,233✔
899
    pTaskInfo->pSubplan->rowsThreshold -= current;
531,791✔
900
  }
901

902
  *hasMore = (pRes != NULL);
482,702,401✔
903
  uint64_t el = (taosGetTimestampUs() - st);
482,537,934✔
904

905
  pTaskInfo->cost.elapsedTime += el;
482,537,934✔
906
  if (NULL == pRes) {
482,381,480✔
907
    *useconds = pTaskInfo->cost.elapsedTime;
433,890,132✔
908
  }
909

910
_end:
482,308,338✔
911
  (void)cleanUpUdfs();
482,382,799✔
912

913
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
482,843,534✔
914
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
482,843,358✔
915
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
916

917
  atomic_store_64(&pTaskInfo->owner, 0);
482,843,358✔
918
  if (code) {
482,843,240✔
919
    pTaskInfo->code = code;
×
920
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
921
  }
922

923
  return pTaskInfo->code;
482,843,240✔
924
}
925

926
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
927
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
928
  SArray*        pList = pTaskInfo->pResultBlockList;
×
929
  size_t         num = taosArrayGetSize(pList);
×
930
  for (int32_t i = 0; i < num; ++i) {
×
931
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
932
    if (p) {
×
933
      blockDataDestroy(*p);
×
934
    }
935
  }
936

937
  taosArrayClear(pTaskInfo->pResultBlockList);
×
938
}
×
939

940
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
127,581,372✔
941
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
127,581,372✔
942
  int64_t        threadId = taosGetSelfPthreadId();
127,581,372✔
943
  int64_t        curOwner = 0;
127,583,468✔
944

945
  *pRes = NULL;
127,583,468✔
946

947
  // todo extract method
948
  taosRLockLatch(&pTaskInfo->lock);
127,583,158✔
949
  bool isKilled = isTaskKilled(pTaskInfo);
127,582,445✔
950
  if (isKilled) {
127,582,736✔
951
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
952

953
    taosRUnLockLatch(&pTaskInfo->lock);
×
954
    return pTaskInfo->code;
×
955
  }
956

957
  if (pTaskInfo->owner != 0) {
127,582,736✔
958
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
959
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
960

961
    taosRUnLockLatch(&pTaskInfo->lock);
×
962
    return pTaskInfo->code;
×
963
  }
964

965
  pTaskInfo->owner = threadId;
127,582,395✔
966
  taosRUnLockLatch(&pTaskInfo->lock);
127,582,767✔
967

968
  if (pTaskInfo->cost.start == 0) {
127,584,441✔
969
    pTaskInfo->cost.start = taosGetTimestampUs();
228,422✔
970
  }
971

972
  // error occurs, record the error code and return to client
973
  int32_t ret = setjmp(pTaskInfo->env);
127,584,441✔
974
  if (ret != TSDB_CODE_SUCCESS) {
127,579,729✔
975
    pTaskInfo->code = ret;
×
976
    (void)cleanUpUdfs();
×
977
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
978
    atomic_store_64(&pTaskInfo->owner, 0);
×
979
    return pTaskInfo->code;
×
980
  }
981

982
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
127,579,729✔
983

984
  int64_t st = taosGetTimestampUs();
127,578,985✔
985
  int32_t code = TSDB_CODE_SUCCESS;
127,578,985✔
986
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
127,578,985✔
987
    pTaskInfo->paramSet = true;
×
988
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
989
  } else {
990
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
127,580,349✔
991
  }
992
  if (code) {
127,527,433✔
993
    pTaskInfo->code = code;
×
994
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
995
  }
996

997
  code = blockDataCheck(*pRes);
127,527,433✔
998
  if (code) {
127,579,157✔
999
    pTaskInfo->code = code;
×
1000
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1001
  }
1002

1003
  uint64_t el = (taosGetTimestampUs() - st);
127,548,572✔
1004

1005
  pTaskInfo->cost.elapsedTime += el;
127,548,572✔
1006
  if (NULL == *pRes) {
127,560,662✔
1007
    *useconds = pTaskInfo->cost.elapsedTime;
18,301,542✔
1008
  }
1009

1010
  (void)cleanUpUdfs();
127,566,156✔
1011

1012
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
127,585,123✔
1013
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
127,585,123✔
1014

1015
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
127,585,123✔
1016
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1017

1018
  atomic_store_64(&pTaskInfo->owner, 0);
127,583,110✔
1019
  return pTaskInfo->code;
127,584,441✔
1020
}
1021

1022
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
122,353,433✔
1023
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
122,353,433✔
1024
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
122,357,132✔
1025
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
122,356,838✔
1026

1027
  if (!tmp) {
122,353,139✔
1028
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1029
    return terrno;
×
1030
  }
1031
  return TSDB_CODE_SUCCESS;
122,353,139✔
1032
}
1033

1034
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1035
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1036
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1037

1038
  if (key->refId < pInfo->refId) {
×
1039
    return -1;
×
1040
  } else if (key->refId > pInfo->refId) {
×
1041
    return 1;
×
1042
  }
1043

1044
  return 0;
×
1045
}
1046

1047
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1048
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1049
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1050
  if (idx >= 0) {
×
1051
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1052
  }
1053
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1054
}
×
1055

1056
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
235,327✔
1057
  if (pTaskInfo->subJobCtx.hasSubJobs) {
235,327✔
1058
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
183,286✔
1059
    if (pTaskInfo->subJobCtx.param) {
183,286✔
1060
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
80,925✔
1061
    }
1062
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
183,286✔
1063
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
183,286✔
1064
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
183,286✔
1065
  }
1066
  
1067
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
235,327✔
1068

1069
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
235,327✔
1070
  for (int32_t i = 0; i < num; ++i) {
264,780✔
1071
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
29,453✔
1072
    if (!pStop) {
29,453✔
1073
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1074
      continue;
×
1075
    }
1076
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
29,453✔
1077
    if (pExchangeInfo) {
29,453✔
1078
      int32_t code = tsem_post(&pExchangeInfo->ready);
29,453✔
1079
      if (code != TSDB_CODE_SUCCESS) {
29,453✔
1080
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1081
      } else {
1082
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
29,453✔
1083
      }
1084
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
29,453✔
1085
      if (code != TSDB_CODE_SUCCESS) {
29,453✔
1086
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1087
      }
1088
    }
1089
  }
1090

1091
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
235,327✔
1092
}
235,327✔
1093

1094
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
235,327✔
1095
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
235,327✔
1096
  if (pTaskInfo == NULL) {
235,327✔
1097
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1098
  }
1099

1100
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
235,327✔
1101

1102
  setTaskKilled(pTaskInfo, rspCode);
235,327✔
1103
  qStopTaskOperators(pTaskInfo);
235,327✔
1104

1105
  return TSDB_CODE_SUCCESS;
235,327✔
1106
}
1107

1108
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1109
  int64_t        st = taosGetTimestampMs();
×
1110
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1111
  if (pTaskInfo == NULL) {
×
1112
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1113
  }
1114

1115
  if (waitDuration > 0) {
×
1116
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1117
  } else {
1118
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1119
  }
1120

1121
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1122

1123
  if (waitDuration > 0) {
×
1124
    while (1) {
1125
      taosWLockLatch(&pTaskInfo->lock);
×
1126
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1127
        taosWUnLockLatch(&pTaskInfo->lock);
×
1128

1129
        taosMsleep(200);
×
1130

1131
        int64_t d = taosGetTimestampMs() - st;
×
1132
        if (d >= waitDuration && waitDuration >= 0) {
×
1133
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1134
          return TSDB_CODE_SUCCESS;
×
1135
        }
1136
      } else {  // not running now
1137
        pTaskInfo->code = rspCode;
×
1138
        taosWUnLockLatch(&pTaskInfo->lock);
×
1139
        return TSDB_CODE_SUCCESS;
×
1140
      }
1141
    }
1142
  }
1143

1144
  int64_t et = taosGetTimestampMs() - st;
×
1145
  if (et < waitDuration) {
×
1146
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1147
    return TSDB_CODE_SUCCESS;
×
1148
  }
1149
  return TSDB_CODE_SUCCESS;
×
1150
}
1151

1152
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1153
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1154
  if (NULL == pTaskInfo) {
×
1155
    return false;
×
1156
  }
1157

1158
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1159
}
1160

1161
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
399,085,601✔
1162
  STaskCostInfo* pSummary = &pTaskInfo->cost;
399,085,601✔
1163
  int64_t        idleTime = pSummary->start - pSummary->created;
399,121,929✔
1164

1165
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
399,079,659✔
1166
  if (pSummary->pRecoder != NULL) {
398,887,136✔
1167
    qDebug(
290,156,607✔
1168
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1169
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1170
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1171
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1172
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1173
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1174
  } else {
1175
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
108,714,288✔
1176
           pSummary->elapsedTime / 1000.0);
1177
  }
1178
}
398,870,912✔
1179

1180

1181
void qDestroyTask(qTaskInfo_t qTaskHandle) {
413,964,387✔
1182
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
413,964,387✔
1183
  if (pTaskInfo == NULL) {
413,964,387✔
1184
    return;
14,885,830✔
1185
  }
1186

1187
  if (pTaskInfo->pRoot != NULL) {
399,078,557✔
1188
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
399,007,060✔
1189
  } else {
1190
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1191
  }
1192

1193
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
399,008,222✔
1194
  doDestroyTask(pTaskInfo);
399,057,494✔
1195
}
1196

1197
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
14,170,534✔
1198
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
14,170,534✔
1199
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
14,170,534✔
1200
}
1201

1202
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
316,046✔
1203
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
316,046✔
1204
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
316,046✔
1205

1206
  while (1) {
310,363✔
1207
    uint16_t type = pOperator->operatorType;
626,409✔
1208
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
626,409✔
1209
      *scanner = pOperator->info;
316,046✔
1210
      break;
316,046✔
1211
    } else {
1212
      pOperator = pOperator->pDownstream[0];
310,363✔
1213
    }
1214
  }
1215
}
316,046✔
1216

1217
void* qExtractReaderFromTmqScanner(void* scanner) {
316,046✔
1218
  SStreamScanInfo* pInfo = scanner;
316,046✔
1219
  return (void*)pInfo->tqReader;
316,046✔
1220
}
1221

1222
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
729,394✔
1223
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
729,394✔
1224
  return pTaskInfo->streamInfo.schema;
729,394✔
1225
}
1226

1227
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
729,394✔
1228
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
729,394✔
1229
  return pTaskInfo->streamInfo.tbName;
729,394✔
1230
}
1231

1232
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
772,116✔
1233
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
772,116✔
1234
  return &pTaskInfo->streamInfo.btMetaRsp;
772,116✔
1235
}
1236

1237
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
19,492,379✔
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,492,379✔
1239
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
19,492,379✔
1240
  return 0;
19,492,379✔
1241
  /*if (code != TSDB_CODE_SUCCESS) {
1242
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1243
    pTaskInfo->code = code;
1244
    T_LONG_JMP(pTaskInfo->env, code);
1245
  }*/
1246
}
1247

1248
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
733,868✔
1249
  memset(pCond, 0, sizeof(SQueryTableDataCond));
733,868✔
1250
  pCond->order = TSDB_ORDER_ASC;
733,868✔
1251
  pCond->numOfCols = pMtInfo->schema->nCols;
733,504✔
1252
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
734,741✔
1253
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
734,145✔
1254
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
735,032✔
1255
    taosMemoryFreeClear(pCond->colList);
1,469✔
1256
    taosMemoryFreeClear(pCond->pSlotList);
×
1257
    return terrno;
×
1258
  }
1259

1260
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
733,868✔
1261
  pCond->suid = pMtInfo->suid;
734,419✔
1262
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
734,741✔
1263
  pCond->startVersion = -1;
735,337✔
1264
  pCond->endVersion = sContext->snapVersion;
733,854✔
1265

1266
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,647,270✔
1267
    SColumnInfo* pColInfo = &pCond->colList[i];
3,911,001✔
1268
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,911,902✔
1269
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,912,529✔
1270
    if (pMtInfo->pExtSchemas != NULL) {
3,911,947✔
1271
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
46,080✔
1272
    }
1273
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,913,139✔
1274
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,912,238✔
1275

1276
    pCond->pSlotList[i] = i;
3,911,597✔
1277
  }
1278

1279
  return TSDB_CODE_SUCCESS;
735,642✔
1280
}
1281

1282
void qStreamSetOpen(qTaskInfo_t tinfo) {
126,082,481✔
1283
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
126,082,481✔
1284
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
126,082,481✔
1285
  pOperator->status = OP_NOT_OPENED;
126,099,622✔
1286
}
126,102,623✔
1287

1288
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
18,699,207✔
1289
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
18,699,207✔
1290
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
18,699,207✔
1291
}
18,700,926✔
1292

1293
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
19,674,181✔
1294
  int32_t        code = TSDB_CODE_SUCCESS;
19,674,181✔
1295
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,674,181✔
1296
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
19,674,181✔
1297

1298
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
19,676,151✔
1299
  const char*    id = GET_TASKID(pTaskInfo);
19,675,272✔
1300

1301
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
19,675,038✔
1302
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
18,405,500✔
1303
    if (pOperator == NULL || code != 0) {
18,403,516✔
1304
      return code;
×
1305
    }
1306

1307
    SStreamScanInfo* pInfo = pOperator->info;
18,403,844✔
1308
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
18,402,909✔
1309
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
18,403,833✔
1310
    walReaderVerifyOffset(pWalReader, pOffset);
18,405,708✔
1311
  }
1312
  // if pOffset equal to current offset, means continue consume
1313
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
19,671,435✔
1314
    return 0;
17,821,037✔
1315
  }
1316

1317
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,850,845✔
1318
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,100,377✔
1319
    if (pOperator == NULL || code != 0) {
1,100,368✔
1320
      return code;
×
1321
    }
1322

1323
    SStreamScanInfo* pInfo = pOperator->info;
1,100,973✔
1324
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,099,438✔
1325
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,100,350✔
1326
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,100,350✔
1327

1328
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,098,815✔
1329
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
878,558✔
1330
      pScanBaseInfo->dataReader = NULL;
877,891✔
1331

1332
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
877,891✔
1333
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
878,583✔
1334
      walReaderVerifyOffset(pWalReader, pOffset);
879,214✔
1335
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
878,909✔
1336
      if (code < 0) {
879,214✔
1337
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
10,890✔
1338
        return code;
10,890✔
1339
      }
1340
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
221,182✔
1341
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1342
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1343
      int64_t uid = pOffset->uid;
221,814✔
1344
      int64_t ts = pOffset->ts;
220,875✔
1345
      int32_t index = 0;
221,182✔
1346

1347
      // this value may be changed if new tables are created
1348
      taosRLockLatch(&pTaskInfo->lock);
221,182✔
1349
      int32_t numOfTables = 0;
220,848✔
1350
      code = tableListGetSize(pTableListInfo, &numOfTables);
220,848✔
1351
      if (code != TSDB_CODE_SUCCESS) {
219,936✔
1352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1353
        taosRUnLockLatch(&pTaskInfo->lock);
×
1354
        return code;
×
1355
      }
1356

1357
      if (uid == 0) {
219,936✔
1358
        if (numOfTables != 0) {
215,786✔
1359
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
38,555✔
1360
          if (!tmp) {
38,555✔
1361
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1362
            taosRUnLockLatch(&pTaskInfo->lock);
×
1363
            return terrno;
×
1364
          }
1365
          if (tmp) uid = tmp->uid;
38,555✔
1366
          ts = INT64_MIN;
38,555✔
1367
          pScanInfo->currentTable = 0;
38,555✔
1368
        } else {
1369
          taosRUnLockLatch(&pTaskInfo->lock);
177,231✔
1370
          qError("no table in table list, %s", id);
177,210✔
1371
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
177,520✔
1372
        }
1373
      }
1374
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
42,705✔
1375

1376
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
43,960✔
1377
             pInfo->pTableScanOp->resultInfo.totalRows);
1378
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
43,960✔
1379

1380
      // start from current accessed position
1381
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1382
      // position, let's find it from the beginning.
1383
      index = tableListFind(pTableListInfo, uid, 0);
44,267✔
1384
      taosRUnLockLatch(&pTaskInfo->lock);
44,267✔
1385

1386
      if (index >= 0) {
44,267✔
1387
        pScanInfo->currentTable = index;
44,267✔
1388
      } else {
1389
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1390
               numOfTables, pScanInfo->currentTable, id);
1391
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1392
      }
1393

1394
      STableKeyInfo keyInfo = {.uid = uid};
44,267✔
1395
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
44,267✔
1396

1397
      // let's start from the next ts that returned to consumer.
1398
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
44,267✔
1399
        pScanBaseInfo->cond.twindows.skey = ts;
913✔
1400
      } else {
1401
        pScanBaseInfo->cond.twindows.skey = ts + 1;
43,354✔
1402
      }
1403
      pScanInfo->scanTimes = 0;
44,267✔
1404

1405
      if (pScanBaseInfo->dataReader == NULL) {
44,267✔
1406
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
77,452✔
1407
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1408
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
38,726✔
1409
        if (code != TSDB_CODE_SUCCESS) {
38,726✔
1410
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1411
          return code;
×
1412
        }
1413

1414
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
38,726✔
1415
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1416
      } else {
1417
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
5,541✔
1418
        if (code != TSDB_CODE_SUCCESS) {
5,541✔
1419
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1420
          return code;
×
1421
        }
1422

1423
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
5,541✔
1424
        if (code != TSDB_CODE_SUCCESS) {
5,541✔
1425
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1426
          return code;
×
1427
        }
1428
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
5,541✔
1429
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1430
      }
1431

1432
      // restore the key value
1433
      pScanBaseInfo->cond.twindows.skey = oldSkey;
44,267✔
1434
    } else {
UNCOV
1435
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1436
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1437
    }
1438

1439
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1440
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
750,468✔
1441
      SStreamRawScanInfo* pInfo = pOperator->info;
736,478✔
1442
      SSnapContext*       sContext = pInfo->sContext;
736,478✔
1443
      SOperatorInfo*      p = NULL;
736,478✔
1444

1445
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
736,478✔
1446
      if (code != 0) {
736,173✔
1447
        return code;
×
1448
      }
1449

1450
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
736,173✔
1451

1452
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
736,173✔
1453
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1454
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1455
      }
1456

1457
      SMetaTableInfo mtInfo = {0};
736,173✔
1458
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
736,173✔
1459
      if (code != 0) {
735,882✔
1460
        destroyMetaTableInfo(&mtInfo);
1461
        return code;
×
1462
      }
1463
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
735,882✔
1464
      pInfo->dataReader = NULL;
734,676✔
1465

1466
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
734,995✔
1467
      tableListClear(pTableListInfo);
734,981✔
1468

1469
      if (mtInfo.uid == 0) {
736,187✔
1470
        destroyMetaTableInfo(&mtInfo);
1471
        goto end;  // no data
836✔
1472
      }
1473

1474
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
735,351✔
1475
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
734,159✔
1476
      if (code != TSDB_CODE_SUCCESS) {
734,405✔
1477
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1478
        destroyMetaTableInfo(&mtInfo);
1479
        return code;
×
1480
      }
1481
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
734,405✔
1482
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,298✔
1483
      } else {
1484
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
733,124✔
1485
      }
1486

1487
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
734,405✔
1488
      if (code != TSDB_CODE_SUCCESS) {
735,642✔
1489
        destroyMetaTableInfo(&mtInfo);
1490
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1491
        return code;
×
1492
      }
1493

1494
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
735,642✔
1495
      if (!pList) {
735,642✔
1496
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1497
        destroyMetaTableInfo(&mtInfo);
1498
        return code;
×
1499
      }
1500
      int32_t size = 0;
735,642✔
1501
      code = tableListGetSize(pTableListInfo, &size);
735,642✔
1502
      if (code != TSDB_CODE_SUCCESS) {
735,642✔
1503
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1504
        destroyMetaTableInfo(&mtInfo);
1505
        return code;
×
1506
      }
1507

1508
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
1,471,284✔
1509
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
735,351✔
1510
      if (code != TSDB_CODE_SUCCESS) {
733,300✔
1511
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1512
        destroyMetaTableInfo(&mtInfo);
1513
        return code;
×
1514
      }
1515

1516
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
733,300✔
1517
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
732,704✔
1518
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1519
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
732,427✔
1520
      pTaskInfo->streamInfo.schema = mtInfo.schema;
733,896✔
1521
      taosMemoryFreeClear(mtInfo.pExtSchemas);
734,187✔
1522

1523
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
734,187✔
1524
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
13,990✔
1525
      SStreamRawScanInfo* pInfo = pOperator->info;
4,740✔
1526
      SSnapContext*       sContext = pInfo->sContext;
4,740✔
1527
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
4,740✔
1528
      if (code != 0) {
4,740✔
1529
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1530
        return code;
×
1531
      }
1532
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
4,740✔
1533
             id);
1534
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
9,250✔
1535
      SStreamRawScanInfo* pInfo = pOperator->info;
9,250✔
1536
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
9,250✔
1537
      pInfo->dataReader = NULL;
9,250✔
1538
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
9,250✔
1539
    }
1540
  }
1541

1542
end:
1,662,660✔
1543
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,663,059✔
1544
  return 0;
1,663,059✔
1545
}
1546

1547
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
361,487,417✔
1548
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
361,487,417✔
1549
  if (pMsg->info.ahandle == NULL) {
361,553,046✔
1550
    rpcFreeCont(pMsg->pCont);
×
1551
    qError("pMsg->info.ahandle is NULL");
×
1552
    return;
×
1553
  }
1554

1555
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
361,430,759✔
1556
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1557

1558
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
361,440,859✔
1559

1560
  if (pMsg->contLen > 0) {
361,322,667✔
1561
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
361,449,220✔
1562
    if (buf.pData == NULL) {
361,185,692✔
1563
      pMsg->code = terrno;
×
1564
    } else {
1565
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
361,185,692✔
1566
    }
1567
  }
1568

1569
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
361,377,766✔
1570
  rpcFreeCont(pMsg->pCont);
361,654,903✔
1571
  destroySendMsgInfo(pSendInfo);
361,600,360✔
1572
}
1573

1574
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1575
  int32_t        code = TSDB_CODE_SUCCESS;
×
1576
  int32_t        lino = 0;
×
1577
  SExecTaskInfo* pTaskInfo = tinfo;
×
1578
  SArray*        plist = NULL;
×
1579

1580
  code = getTableListInfo(pTaskInfo, &plist);
×
1581
  if (code || plist == NULL) {
×
1582
    return NULL;
×
1583
  }
1584

1585
  // only extract table in the first elements
1586
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1587

1588
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1589
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1590

1591
  int32_t numOfTables = 0;
×
1592
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1593
  QUERY_CHECK_CODE(code, lino, _end);
×
1594

1595
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1596
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1597
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1598
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1599
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1600
  }
1601

1602
  taosArrayDestroy(plist);
×
1603

1604
_end:
×
1605
  if (code != TSDB_CODE_SUCCESS) {
×
1606
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1607
    T_LONG_JMP(pTaskInfo->env, code);
×
1608
  }
1609
  return pUidList;
×
1610
}
1611

1612
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,353,700✔
1613
  int32_t        code = TSDB_CODE_SUCCESS;
3,353,700✔
1614
  int32_t        lino = 0;
3,353,700✔
1615
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,353,700✔
1616

1617
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,355,303✔
1618
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1619
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1620

1621
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1622
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1623
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,353,692✔
1624
    STableScanInfo* pScanInfo = pOperator->info;
1,677,924✔
1625

1626
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,677,924✔
1627
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,678,461✔
1628
  } else {
1629
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,676,850✔
1630
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,676,850✔
1631
    }
1632
  }
1633

1634
_end:
×
1635
  if (code != TSDB_CODE_SUCCESS) {
3,354,221✔
1636
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1637
  }
1638
  return code;
3,353,147✔
1639
}
1640

1641
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,676,850✔
1642
  if (pList == NULL) {
1,676,850✔
1643
    return TSDB_CODE_INVALID_PARA;
×
1644
  }
1645

1646
  *pList = NULL;
1,676,850✔
1647
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,677,924✔
1648
  if (pArray == NULL) {
1,676,313✔
1649
    return terrno;
×
1650
  }
1651

1652
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,676,313✔
1653
  if (code == 0) {
1,676,297✔
1654
    *pList = pArray;
1,676,297✔
1655
  } else {
1656
    taosArrayDestroy(pArray);
×
1657
  }
1658
  return code;
1,675,768✔
1659
}
1660

1661
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1662
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1663
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1664
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1665
  }
1666
  return 0;
×
1667
}
1668

1669
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1670
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1671
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1672
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1673
  }
1674
  return 0;
×
1675
}
1676

1677
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1678
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1679

1680
  int32_t code = pTaskInfo->code;
×
1681
  pTaskInfo->code = 0;
×
1682
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1683
}
×
1684

1685
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1686
  int32_t code = 0;
×
1687
  return code;
×
1688
}
1689

1690
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1691
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1692
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1693
  return code;
×
1694
}
1695

1696
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
31,891,070✔
1697
  int32_t code = 0;
31,891,070✔
1698

1699
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
31,891,070✔
1700
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
31,887,948✔
1701

1702
  if (pOper->fpSet.resetStateFn) {
31,887,729✔
1703
    code = pOper->fpSet.resetStateFn(pOper);
31,888,575✔
1704
  }
1705
  pOper->status = OP_NOT_OPENED;
31,888,046✔
1706
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
55,466,078✔
1707
    code = clearStatesForOperator(pOper->pDownstream[i]);
23,571,645✔
1708
  }
1709
  return code;
31,895,513✔
1710
}
1711

1712
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,321,534✔
1713
  int32_t        code = 0;
8,321,534✔
1714
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,321,534✔
1715
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,321,534✔
1716
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,321,534✔
1717
  code = clearStatesForOperator(pOper);
8,321,534✔
1718
  return code;
8,321,534✔
1719
}
1720

1721
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,894,457✔
1722
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,894,457✔
1723
  int64_t        threadId = taosGetSelfPthreadId();
10,894,457✔
1724
  int64_t        curOwner = 0;
10,894,036✔
1725

1726
  *ppRes = NULL;
10,894,036✔
1727

1728
  // todo extract method
1729
  taosRLockLatch(&pTaskInfo->lock);
10,894,036✔
1730
  bool isKilled = isTaskKilled(pTaskInfo);
10,894,248✔
1731
  if (isKilled) {
10,894,036✔
1732
    // clearStreamBlock(pTaskInfo->pRoot);
1733
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1734

1735
    taosRUnLockLatch(&pTaskInfo->lock);
×
1736
    return pTaskInfo->code;
×
1737
  }
1738

1739
  if (pTaskInfo->owner != 0) {
10,894,036✔
1740
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1741
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1742

1743
    taosRUnLockLatch(&pTaskInfo->lock);
×
1744
    return pTaskInfo->code;
×
1745
  }
1746

1747
  pTaskInfo->owner = threadId;
10,894,036✔
1748
  taosRUnLockLatch(&pTaskInfo->lock);
10,894,248✔
1749

1750
  if (pTaskInfo->cost.start == 0) {
10,894,033✔
1751
    pTaskInfo->cost.start = taosGetTimestampUs();
211,343✔
1752
  }
1753

1754
  // error occurs, record the error code and return to client
1755
  int32_t ret = setjmp(pTaskInfo->env);
10,894,033✔
1756
  if (ret != TSDB_CODE_SUCCESS) {
13,551,928✔
1757
    pTaskInfo->code = ret;
2,658,104✔
1758
    (void)cleanUpUdfs();
2,658,104✔
1759
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,658,313✔
1760
    atomic_store_64(&pTaskInfo->owner, 0);
2,658,313✔
1761
    return pTaskInfo->code;
2,658,313✔
1762
  }
1763

1764
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,893,824✔
1765

1766
  int64_t st = taosGetTimestampUs();
10,894,457✔
1767

1768
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,894,457✔
1769
  if (code) {
8,236,144✔
1770
    pTaskInfo->code = code;
×
1771
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1772
  } else {
1773
    *finished = *ppRes == NULL;
8,236,144✔
1774
    code = blockDataCheck(*ppRes);
8,236,144✔
1775
  }
1776
  if (code) {
8,236,144✔
1777
    pTaskInfo->code = code;
×
1778
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1779
  }
1780

1781
  uint64_t el = (taosGetTimestampUs() - st);
8,235,928✔
1782

1783
  pTaskInfo->cost.elapsedTime += el;
8,235,928✔
1784
  if (NULL == *ppRes) {
8,236,144✔
1785
    *useconds = pTaskInfo->cost.elapsedTime;
5,131,284✔
1786
  }
1787

1788
  (void)cleanUpUdfs();
8,236,144✔
1789

1790
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,236,144✔
1791
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,236,144✔
1792

1793
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
8,235,896✔
1794
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1795

1796
  atomic_store_64(&pTaskInfo->owner, 0);
8,235,896✔
1797
  return pTaskInfo->code;
8,236,144✔
1798
}
1799

1800
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1801
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1802
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1803
// }
1804

1805
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
229,335✔
1806
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1807
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1808
  int32_t code = 0;                                        
229,335✔
1809
  if (*pTableListInfo != NULL) {
229,335✔
1810
    qDebug("table list already exists, no need to create again");
×
1811
    goto end;
×
1812
  }
1813
  STableListInfo* pList = tableListCreate();
229,335✔
1814
  if (pList == NULL) {
229,335✔
1815
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1816
    code = terrno;
×
1817
    goto end;
×
1818
  }
1819

1820
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
229,335✔
1821
  SReadHandle    pHandle = {.vnode = pVnode};
229,335✔
1822
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
229,335✔
1823

1824
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
229,335✔
1825
  if (code != 0) {
229,335✔
1826
    tableListDestroy(pList);
×
1827
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1828
    goto end;
×
1829
  }
1830
  *pTableListInfo = pList;
229,335✔
1831

1832
end:
229,335✔
1833
  return 0;
229,335✔
1834
}
1835

1836
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
32,919✔
1837
  bool   listAdded = false;
32,919✔
1838
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
32,919✔
1839
  if (code == 0 && !listAdded) {
32,919✔
1840
    int32_t numOfTables = taosArrayGetSize(pUidList);
26,926✔
1841
    for (int i = 0; i < numOfTables; i++) {
53,852✔
1842
      void* tmp = taosArrayGet(pUidList, i);
26,926✔
1843
      if (tmp == NULL) {
26,926✔
1844
        return terrno;
×
1845
      }
1846
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
26,926✔
1847

1848
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
26,926✔
1849
      if (p == NULL) {
26,926✔
1850
        return terrno;
×
1851
      }
1852
    }
1853
  }
1854
  return code;
32,919✔
1855
}
1856

1857
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
28,473✔
1858
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1859
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1860
  int32_t code = TSDB_CODE_SUCCESS;
28,473✔
1861
  STableListInfo* pList = tableListCreate();
28,473✔
1862
  if (pList == NULL) {
28,473✔
1863
    code = terrno;
×
1864
    goto end;
×
1865
  }
1866
  SArray* uidListCopy = taosArrayDup(uidList, NULL);
28,473✔
1867
  if (uidListCopy == NULL) {
28,473✔
1868
    code = terrno;
×
1869
    goto end;
×
1870
  }
1871
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
28,473✔
1872
  SReadHandle    pHandle = {.vnode = pVnode};
28,473✔
1873

1874
  pList->idInfo.suid = suid;
28,473✔
1875
  pList->idInfo.tableType = TD_SUPER_TABLE;
28,473✔
1876
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
28,473✔
1877
  if (code != TSDB_CODE_SUCCESS) {
28,473✔
1878
    goto end;
×
1879
  }                                              
1880
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
28,473✔
1881
  if (code != TSDB_CODE_SUCCESS) {
28,473✔
1882
    goto end;
×
1883
  }
1884
  *tableList = pList->pTableList;
28,473✔
1885
  pList->pTableList = NULL;
28,473✔
1886

1887
  taosArrayClear(uidList);
28,473✔
1888
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
56,946✔
1889
    void* tmp = taosArrayGet(uidListCopy, i);
28,473✔
1890
    if (tmp == NULL) {
28,473✔
1891
      continue;
×
1892
    }
1893
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
28,473✔
1894
    if (slot == NULL) {
28,473✔
1895
      if (taosArrayPush(uidList, tmp) == NULL) {
3,363✔
1896
        code = terrno;
×
1897
        goto end;
×
1898
      }
1899
    }
1900
  }
1901
end:
28,473✔
1902
  taosArrayDestroy(uidListCopy);
28,473✔
1903
  tableListDestroy(pList);
28,473✔
1904
  return code;
28,473✔
1905
}
1906

1907
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1908
//   int32_t index = -1;
1909
//   (void)taosThreadRwlockRdlock(lock);
1910
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1911
//     index = 0;
1912
//     goto end;
1913
//   }
1914
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1915
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1916

1917
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1918
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1919
//       index = i;
1920
//       goto end;
1921
//     }
1922
//   }
1923
// end:
1924
//   (void)taosThreadRwlockUnlock(lock);
1925
//   return index;
1926
// }
1927

1928
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
229,335✔
1929
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
229,335✔
1930
  STableListInfo* pList = pTableListInfo;
229,335✔
1931
  return pList->pTableList;
229,335✔
1932
}
1933

1934
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,334,293✔
1935

1936
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,187,120✔
1937
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,187,120✔
1938
  qDestroyTask(tInfo);
2,187,120✔
1939
}
2,187,120✔
1940

1941
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
589,655✔
1942
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
589,655✔
1943
}
1944

1945
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
642,680✔
1946
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1947
  int32_t      code = 0;
642,680✔
1948
  SNode*       pNode = 0;
642,680✔
1949
  SNodeList*   pList = NULL;
642,680✔
1950
  SExprInfo*   pExprInfo = NULL;
642,680✔
1951
  int32_t      numOfExprs = 1;
642,680✔
1952
  int32_t*     offset = 0;
642,680✔
1953
  STargetNode* pTargetNode = NULL;
642,680✔
1954
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
642,680✔
1955
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
642,469✔
1956

1957
  if (code == 0) {
642,680✔
1958
    pTargetNode->dataBlockId = 0;
642,680✔
1959
    pTargetNode->pExpr = pNode;
642,680✔
1960
    pTargetNode->slotId = 0;
642,680✔
1961
  }
1962
  if (code == 0) {
642,680✔
1963
    code = nodesMakeList(&pList);
642,680✔
1964
  }
1965
  if (code == 0) {
642,680✔
1966
    code = nodesListAppend(pList, (SNode*)pTargetNode);
642,680✔
1967
  }
1968
  if (code == 0) {
642,680✔
1969
    pNode = NULL;
642,680✔
1970
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
642,680✔
1971
  }
1972

1973
  if (code == 0) {
642,459✔
1974
    const char* pVal = NULL;
642,459✔
1975
    int32_t     len = 0;
642,459✔
1976
    SNode*      pSclNode = NULL;
642,459✔
1977
    switch (pExprInfo->pExpr->nodeType) {
642,459✔
1978
      case QUERY_NODE_FUNCTION:
642,459✔
1979
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
642,459✔
1980
        break;
642,680✔
1981
      case QUERY_NODE_OPERATOR:
×
1982
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1983
        break;
×
1984
      default:
×
1985
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1986
        break;
×
1987
    }
1988
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
642,680✔
1989
    SSDataBlock block = {0};
642,680✔
1990
    block.info.rows = 1;
642,680✔
1991
    SSDataBlock* pBlock = &block;
642,680✔
1992
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
642,680✔
1993
    if (tmp == NULL) {
642,680✔
1994
      code = terrno;
×
1995
    }
1996
    if (code == 0) {
642,680✔
1997
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
642,680✔
1998
      gTaskScalarExtra.pStreamRange = NULL;
642,680✔
1999
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
642,680✔
2000
    }
2001
    taosArrayDestroy(pBlockList);
642,680✔
2002
  }
2003
  nodesDestroyList(pList);
642,680✔
2004
  destroyExprInfo(pExprInfo, numOfExprs);
642,468✔
2005
  taosMemoryFreeClear(pExprInfo);
642,680✔
2006
  return code;
642,247✔
2007
}
2008

2009
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,029,725✔
2010
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,029,725✔
2011
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,029,725✔
2012
  int32_t        code = 0;
2,029,725✔
2013
  SNode*         pNode = NULL;
2,029,725✔
2014
  if (!pForceOutputCols) return 0;
2,029,725✔
2015
  if (!*pRes) {
53,025✔
2016
    code = createDataBlock(pRes);
53,025✔
2017
  }
2018

2019
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
53,025✔
2020
    int32_t idx = 0;
53,025✔
2021
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
211,911✔
2022
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
158,886✔
2023
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
158,886✔
2024
      colInfo.info.precision = pCol->type.precision;
158,886✔
2025
      colInfo.info.scale = pCol->type.scale;
158,886✔
2026
      code = blockDataAppendColInfo(*pRes, &colInfo);
158,886✔
2027
      if (code != 0) break;
158,886✔
2028
    }
2029
  }
2030

2031
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
53,025✔
2032
  if (code != TSDB_CODE_SUCCESS) {
53,025✔
2033
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2034
    return code;
×
2035
  }
2036

2037
  // loop all exprs for force output, execute all exprs
2038
  int32_t idx = 0;
53,025✔
2039
  int32_t rowIdx = (*pRes)->info.rows;
53,025✔
2040
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
53,025✔
2041
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
53,025✔
2042
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
211,911✔
2043
    SScalarParam   dst = {0};
158,886✔
2044
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
158,886✔
2045
    code = nodesStringToNode(pCol->expr, &pNode);
158,886✔
2046
    if (code != 0) break;
158,886✔
2047
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
158,886✔
2048
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
158,886✔
2049
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
105,861✔
2050
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
105,861✔
2051
    } else {
2052
      dst.columnData = pInfo;
53,025✔
2053
      dst.numOfRows = rowIdx;
53,025✔
2054
      dst.colAlloced = false;
53,025✔
2055
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
53,025✔
2056
    }
2057
    ++idx;
158,886✔
2058
    // TODO sclFreeParam(&dst);
2059
    nodesDestroyNode(pNode);
158,886✔
2060
    if (code != 0) break;
158,886✔
2061
  }
2062
  if (code == TSDB_CODE_SUCCESS) {
53,025✔
2063
    (*pRes)->info.rows++;
53,025✔
2064
  }
2065
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
53,025✔
2066
  return code;
53,025✔
2067
}
2068

2069
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
230,744✔
2070
  int32_t      code = 0;
230,744✔
2071
  const char*  pVal = NULL;
230,744✔
2072
  SScalarParam dst = {0};
230,744✔
2073
  int32_t      len = 0;
230,744✔
2074
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
230,744✔
2075
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
230,744✔
2076
  // execute the expr
2077
  switch (pExpr->type) {
230,744✔
2078
    case QUERY_NODE_VALUE: {
×
2079
      SValueNode* pValue = (SValueNode*)pExpr;
×
2080
      int32_t     type = pValue->node.resType.type;
×
2081
      if (!IS_STR_DATA_TYPE(type)) {
×
2082
        qError("invalid sub tb expr with non-str type");
×
2083
        code = TSDB_CODE_INVALID_PARA;
×
2084
        break;
×
2085
      }
2086
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2087
      if (pTmp == NULL) {
×
2088
        qError("invalid sub tb expr with null value");
×
2089
        code = TSDB_CODE_INVALID_PARA;
×
2090
        break;
×
2091
      }
2092
      pVal = varDataVal(pTmp);
×
2093
      len = varDataLen(pTmp);
×
2094
    } break;
×
2095
    case QUERY_NODE_FUNCTION: {
230,744✔
2096
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
230,744✔
2097
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
230,744✔
2098
        qError("invalid sub tb expr with non-str type func");
×
2099
        code = TSDB_CODE_INVALID_PARA;
×
2100
        break;
×
2101
      }
2102
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
230,744✔
2103
      if (!pCol) {
230,744✔
2104
        code = terrno;
×
2105
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2106
        break;
×
2107
      }
2108

2109
      pCol->hasNull = true;
230,744✔
2110
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
230,744✔
2111
      pCol->info.colId = 0;
230,342✔
2112
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
230,528✔
2113
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
230,744✔
2114
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
230,744✔
2115
      code = colInfoDataEnsureCapacity(pCol, 1, true);
230,528✔
2116
      if (code != 0) {
230,342✔
2117
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2118
        taosMemoryFree(pCol);
×
2119
        break;
×
2120
      }
2121
      dst.columnData = pCol;
230,342✔
2122
      dst.numOfRows = 1;
230,342✔
2123
      dst.colAlloced = true;
230,342✔
2124
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
230,342✔
2125
      if (colDataIsNull_var(dst.columnData, 0)) {
230,744✔
2126
        qInfo("invalid sub tb expr with null value");
3,200✔
2127
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
3,200✔
2128
      }
2129
      if (code == 0) {
230,744✔
2130
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
227,544✔
2131
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
227,544✔
2132
      }
2133
    } break;
230,744✔
2134
    default:
×
2135
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2136
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2137
      break;
×
2138
  }
2139
  if (code == 0) {
230,744✔
2140
    if (!pVal || len == 0) {
227,544✔
2141
      qError("tbname generated with no characters which is not allowed");
×
2142
      code = TSDB_CODE_INVALID_PARA;
×
2143
    }
2144
    if(len > TSDB_TABLE_NAME_LEN - 1) {
227,544✔
2145
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
426✔
2146
      len = TSDB_TABLE_NAME_LEN - 1;
426✔
2147
    }
2148

2149
    memcpy(tbname, pVal, len);
227,544✔
2150
    tbname[len] = '\0';  // ensure null terminated
227,544✔
2151
    if (NULL != strchr(tbname, '.')) {
227,544✔
2152
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2153
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2154
    }
2155
  }
2156
  // TODO free dst
2157
  sclFreeParam(&dst);
230,744✔
2158
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
230,744✔
2159
  return code;
230,744✔
2160
}
2161

2162
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
204,864✔
2163
  if (pParam) {
204,864✔
2164
    if (pParam->tbname) {
204,864✔
2165
      taosMemFree(pParam->tbname);
204,864✔
2166
      pParam->tbname = NULL;
204,864✔
2167
    }
2168
    if (pParam->stbname) {
204,864✔
2169
      taosMemFree(pParam->stbname);
204,864✔
2170
      pParam->stbname = NULL;
204,864✔
2171
    }
2172
    if (pParam->dbFName) {
204,864✔
2173
      taosMemFree(pParam->dbFName);
204,864✔
2174
      pParam->dbFName = NULL;
204,864✔
2175
    }
2176
    if (pParam->pFields) {
204,864✔
2177
      taosArrayDestroy(pParam->pFields);
204,864✔
2178
      pParam->pFields = NULL;
204,864✔
2179
    }
2180
    if (pParam->pTagFields) {
204,864✔
2181
      taosArrayDestroy(pParam->pTagFields);
124,999✔
2182
      pParam->pTagFields = NULL;
124,999✔
2183
    }
2184
    if (pParam->colCids) {
204,864✔
2185
      taosArrayDestroy(pParam->colCids);
1,718✔
2186
      pParam->colCids = NULL;
1,718✔
2187
    }
2188
    if (pParam->tagCids) {
204,864✔
2189
      taosArrayDestroy(pParam->tagCids);
1,097✔
2190
      pParam->tagCids = NULL;
1,097✔
2191
    }
2192
    taosMemFree(pParam);
204,864✔
2193
  }
2194
}
204,864✔
2195

2196
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
204,653✔
2197
  int32_t code = 0, lino = 0;
204,653✔
2198
  if (ppDst == NULL || pSrc == NULL) {
204,653✔
2199
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2200
  }
2201
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
204,653✔
2202
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
204,864✔
2203

2204
  (*ppDst)->suid = pSrc->suid;
204,864✔
2205
  (*ppDst)->sver = pSrc->sver;
204,864✔
2206
  (*ppDst)->tbType = pSrc->tbType;
204,864✔
2207
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
204,864✔
2208
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
204,864✔
2209

2210
  if (pSrc->stbname) {
204,864✔
2211
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
204,864✔
2212
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
204,864✔
2213
  }
2214

2215
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
204,864✔
2216
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
204,864✔
2217

2218
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
204,625✔
2219

2220
  if (pSrc->pFields && pSrc->pFields->size > 0) {
204,864✔
2221
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
204,864✔
2222
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
204,864✔
2223
  } else {
2224
    (*ppDst)->pFields = NULL;
×
2225
  }
2226
  
2227
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
204,864✔
2228
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
124,999✔
2229
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
124,999✔
2230
  } else {
2231
    (*ppDst)->pTagFields = NULL;
79,865✔
2232
  }
2233

2234
  if (pSrc->colCids && pSrc->colCids->size > 0) {
204,864✔
2235
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,718✔
2236
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,718✔
2237
  } else {
2238
    (*ppDst)->colCids = NULL;
203,146✔
2239
  }
2240

2241
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
204,864✔
2242
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,097✔
2243
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,097✔
2244
  } else {
2245
    (*ppDst)->tagCids = NULL;
203,767✔
2246
  }
2247

2248
_exit:
204,625✔
2249

2250
  if (code != 0) {
204,625✔
2251
    if (*ppDst) {
×
2252
      destroyStreamInserterParam(*ppDst);
×
2253
      *ppDst = NULL;
×
2254
    }
2255
    
2256
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2257
  }
2258
  return code;
204,864✔
2259
}
2260

2261
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
245✔
2262
  return doDropStreamTable(pMsgCb, pOutput, pReq);
245✔
2263
}
2264

2265
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2266
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2267
}
2268

2269
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
4,446✔
2270
  int32_t         code = TSDB_CODE_SUCCESS;
4,446✔
2271
  STableListInfo* pList = tableListCreate();
4,446✔
2272
  if (pList == NULL) {
4,446✔
2273
    code = terrno;
×
2274
    goto end;
×
2275
  }
2276
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2277
  // if (uidListCopy == NULL) {
2278
  //   code = terrno;
2279
  //   goto end;
2280
  // }
2281

2282
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,446✔
2283
  pList->idInfo.suid = suid;
4,446✔
2284
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,446✔
2285
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,446✔
2286
  if (code != TSDB_CODE_SUCCESS) {
4,446✔
2287
    goto end;
×
2288
  }
2289
  // taosArrayClear(uidList);
2290
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2291
  //   void* tmp = taosArrayGet(uidListCopy, i);
2292
  //   if (tmp == NULL) {
2293
  //     continue;
2294
  //   }
2295
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2296
  //   if (slot == NULL) {
2297
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2298
  //       code = terrno;
2299
  //       goto end;
2300
  //     }
2301
  //   }
2302
  // }
2303
end:
4,446✔
2304
  // taosArrayDestroy(uidListCopy);
2305
  tableListDestroy(pList);
4,446✔
2306
  return code;
4,446✔
2307
}
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