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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

68.88
/source/libs/executor/src/executor.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include <stdint.h>
18
#include "cmdnodes.h"
19
#include "dataSinkInt.h"
20
#include "executil.h"
21
#include "executorInt.h"
22
#include "libs/new-stream/stream.h"
23
#include "operator.h"
24
#include "osMemPool.h"
25
#include "osMemory.h"
26
#include "planner.h"
27
#include "query.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "streamexecutorInt.h"
31
#include "taosdef.h"
32
#include "tarray.h"
33
#include "tdatablock.h"
34
#include "tref.h"
35
#include "trpc.h"
36
#include "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) {
554,497,430✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
554,497,430✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
554,497,430✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
554,612,781✔
47
}
554,345,994✔
48

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

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

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

70
static void initRefPool() {
673,252✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
673,252✔
72
  (void)atexit(cleanupRefPool);
673,252✔
73
}
673,252✔
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) {
16,259,474✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
16,259,474✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
16,260,159✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
4,804,332✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
4,804,332✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
4,804,525✔
162
      if (pScanInfo->base.dataReader != NULL) {
4,804,421✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
57,042✔
164
        if (code) {
57,042✔
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);
11,455,248✔
172
  }
173

174
  return 0;
10,697,011✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
10,696,508✔
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) {
139,619✔
213
  if (msg == NULL) {  // create raw scan
139,619✔
214
    SExecTaskInfo* pTaskInfo = NULL;
24,420✔
215

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

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

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

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

248
  return pTaskInfo;
117,056✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
727,082✔
252
  if (streamInserterParam == NULL) {
727,082✔
253
    return TSDB_CODE_SUCCESS;
395,029✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
332,053✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
332,053✔
323
    if (code) {
332,053✔
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,
726,672✔
328
         tstrerror(code));
329

330
_error:
136,545✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
5,174,731✔
342
  if (pInfo == NULL) {
5,174,731✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
5,174,731✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
5,174,731✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
5,174,731✔
348
    return false;
9,276✔
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
5,165,455✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
4,841,473✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
10,006,928✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node);
354
}
355

356
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
4,495,637✔
357
  if (pHandle == NULL || pScanBaseInfo == NULL) {
4,495,637✔
358
    return;
×
359
  }
360

361
  pScanBaseInfo->readHandle.uid = pHandle->uid;
4,495,637✔
362
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
4,495,637✔
363
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
4,495,637✔
364
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
4,495,637✔
365
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
4,495,637✔
366
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
4,495,637✔
367
}
368

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

376
  void*           info = pOperator->info;
5,165,455✔
377
  STableScanBase* pScanBaseInfo = NULL;
5,165,455✔
378

379
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
5,165,455✔
380
    pScanBaseInfo = &((STableScanInfo*)info)->base;
323,982✔
381
    setReadHandle(handle, pScanBaseInfo);
323,982✔
382
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
4,841,473✔
383
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
4,171,655✔
384
    setReadHandle(handle, pScanBaseInfo);
4,171,655✔
385
  }
386

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

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

398
  *pTaskInfo = NULL;
727,082✔
399

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

415
  return code;
726,672✔
416
}
417

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

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

431
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
61,809✔
432

433
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
61,809✔
434
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
61,809✔
435

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

442
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
61,809✔
443

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

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

453
  locked = 1;
61,809✔
454
  for (int32_t i = 0; i < numOfUids; ++i) {
124,383✔
455
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
62,574✔
456
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
62,574✔
457

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

464
    tDecoderClear(&mr.coder);
62,574✔
465

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

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

493
  pAPI->metaReaderFn.clearReader(&mr);
61,809✔
494
  locked = 0;
61,809✔
495

496
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
124,383✔
497
    bool     qualified = false;
62,574✔
498
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
62,574✔
499
    if (t == NULL) {
62,574✔
500
      continue;
×
501
    }
502

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

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

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

520
  // handle multiple partition
521

522
_end:
61,809✔
523

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

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

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

542
  if (isAdd) {
62,405✔
543
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
61,809✔
544
  }
545

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

553
  SStreamScanInfo* pScanInfo = pInfo->info;
62,405✔
554
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
62,405✔
555
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
125,131✔
556
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
62,726✔
557
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
62,726✔
558
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
62,726✔
559
    }
560
  }
561

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

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

585
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
61,809✔
586
    taosWLockLatch(&pTaskInfo->lock);
61,809✔
587

588
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
97,212✔
589
      uint64_t* uid = taosArrayGet(qa, i);
35,403✔
590
      if (!uid) {
35,403✔
591
        taosMemoryFree(keyBuf);
×
592
        taosArrayDestroy(qa);
×
593
        taosWUnLockLatch(&pTaskInfo->lock);
×
594
        return terrno;
×
595
      }
596
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
35,403✔
597

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

613
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
35,403✔
614
      if (code != TSDB_CODE_SUCCESS) {
35,403✔
615
        taosMemoryFree(keyBuf);
×
616
        taosArrayDestroy(qa);
×
617
        taosWUnLockLatch(&pTaskInfo->lock);
×
618
        return code;
×
619
      }
620
    }
621

622
    taosWUnLockLatch(&pTaskInfo->lock);
61,809✔
623
    if (keyBuf != NULL) {
61,809✔
624
      taosMemoryFree(keyBuf);
×
625
    }
626

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

635
  return code;
62,405✔
636
}
637

638
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
474,448,648✔
639
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
640
                                    int32_t idx, bool* tbGet) {
641
  *tbGet = false;
474,448,648✔
642

643
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
474,484,192✔
644
    return TSDB_CODE_INVALID_PARA;
×
645
  }
646
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
474,542,716✔
647

648
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
474,542,716✔
649
    return TSDB_CODE_SUCCESS;
263,642,708✔
650
  }
651

652
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
210,869,602✔
653
  if (!pSchemaInfo) {
210,647,904✔
654
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
655
    return terrno;
×
656
  }
657

658
  *sversion = pSchemaInfo->sw->version;
210,647,904✔
659
  *tversion = pSchemaInfo->tversion;
210,936,763✔
660
  *rversion = pSchemaInfo->rversion;
210,900,899✔
661
  if (pSchemaInfo->dbname) {
210,941,361✔
662
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
210,976,164✔
663
  } else {
664
    dbName[0] = 0;
×
665
  }
666
  if (pSchemaInfo->tablename) {
211,079,437✔
667
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
211,003,185✔
668
  } else {
669
    tableName[0] = 0;
23,768✔
670
  }
671

672
  *tbGet = true;
211,075,360✔
673

674
  return TSDB_CODE_SUCCESS;
211,035,071✔
675
}
676

677
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
263,521,238✔
678

679
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
680
  if (NULL == pParam) {
×
681
    return;
×
682
  }
683
  freeOperatorParam(pParam, OP_GET_PARAM);
×
684
}
685

686
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
7,883,024✔
687
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
7,883,024✔
688
  ((SExecTaskInfo*)tinfo)->paramSet = false;
7,884,677✔
689
}
7,885,228✔
690

691
int32_t qExecutorInit(void) {
4,804,446✔
692
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,804,446✔
693
  return TSDB_CODE_SUCCESS;
4,804,446✔
694
}
695

696
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
46,998,656✔
697
  int32_t        code = TSDB_CODE_SUCCESS;
46,998,656✔
698
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
46,998,656✔
699
  if (pTask->pWorkerCb) {
46,998,656✔
700
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
46,999,789✔
701
    if (code != TSDB_CODE_SUCCESS) {
47,004,210✔
702
      pTask->code = code;
×
703
      return pTask->code;
×
704
    }
705
  }
706

707
  code = tsem_wait(pSem);
47,007,544✔
708
  if (code != TSDB_CODE_SUCCESS) {
47,004,210✔
709
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
710
    pTask->code = code;
×
711
    return pTask->code;
×
712
  }
713

714
  if (pTask->pWorkerCb) {
47,004,210✔
715
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
47,004,150✔
716
    if (code != TSDB_CODE_SUCCESS) {
47,003,047✔
717
      pTask->code = code;
×
718
      return pTask->code;
×
719
    }
720
  }
721
  return TSDB_CODE_SUCCESS;
47,003,667✔
722
}
723

724
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
270,393,290✔
725
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
726
                        EOPTR_EXEC_MODEL model, SArray* subEndPoints) {
727
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
270,393,290✔
728
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
270,393,290✔
729

730
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
270,380,897✔
731
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints));
732

733
  readHandle->uid = 0;
270,404,633✔
734
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
270,446,064✔
735
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
270,296,633✔
736
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
5,041,105✔
737
    goto _error;
4,955,583✔
738
  }
739
    
740
  if (handle) {
265,321,455✔
741
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
265,231,369✔
742
    void*           pSinkManager = NULL;
265,331,346✔
743
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
265,208,602✔
744
    if (code != TSDB_CODE_SUCCESS) {
264,702,932✔
745
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
746
      goto _error;
×
747
    }
748

749
    void* pSinkParam = NULL;
264,702,932✔
750
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
265,138,402✔
751
    if (code != TSDB_CODE_SUCCESS) {
264,832,383✔
752
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
753
      taosMemoryFree(pSinkManager);
×
754
      goto _error;
×
755
    }
756

757
    SDataSinkNode* pSink = NULL;
264,832,383✔
758
    if (readHandle->localExec) {
265,086,327✔
759
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
5,720✔
760
      if (code != TSDB_CODE_SUCCESS) {
5,720✔
761
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
762
               (*pTask)->id.str);
763
        taosMemoryFree(pSinkManager);
×
764
        goto _error;
×
765
      }
766
    }
767

768
    // pSinkParam has been freed during create sinker.
769
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
264,793,582✔
770
                              (*pTask)->id.str, pSubplan->processOneBlock);
265,235,408✔
771
    if (code) {
264,942,045✔
772
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
657✔
773
    }
774
  }
775

776
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
265,203,627✔
777
    taskId, pSubplan->id.queryId, tstrerror(code), (int32_t)taosArrayGetSize((*pTask)->subJobCtx.subEndPoints));
778

779
_error:
118,351,855✔
780
  // if failed to add ref for all tables in this query, abort current query
781
  return code;
270,373,988✔
782
}
783

784
static void freeBlock(void* param) {
×
785
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
786
  blockDataDestroy(pBlock);
×
787
}
×
788

789
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
289,058,926✔
790
                     bool processOneBlock) {
791
  int32_t        code = TSDB_CODE_SUCCESS;
289,058,926✔
792
  int32_t        lino = 0;
289,058,926✔
793
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
289,058,926✔
794
  int64_t        threadId = taosGetSelfPthreadId();
289,058,926✔
795

796
  if (pLocal) {
289,022,399✔
797
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
283,454,833✔
798
  }
799

800
  taosArrayClear(pResList);
288,987,568✔
801

802
  int64_t curOwner = 0;
288,930,089✔
803
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
288,930,089✔
804
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
805
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
806
    return pTaskInfo->code;
×
807
  }
808

809
  if (pTaskInfo->cost.start == 0) {
288,909,008✔
810
    pTaskInfo->cost.start = taosGetTimestampUs();
263,727,920✔
811
  }
812

813
  if (isTaskKilled(pTaskInfo)) {
289,155,808✔
814
    atomic_store_64(&pTaskInfo->owner, 0);
120✔
815
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
120✔
816
    return pTaskInfo->code;
120✔
817
  }
818

819
  // error occurs, record the error code and return to client
820
  int32_t ret = setjmp(pTaskInfo->env);
288,912,239✔
821
  if (ret != TSDB_CODE_SUCCESS) {
293,070,583✔
822
    pTaskInfo->code = ret;
4,375,334✔
823
    (void)cleanUpUdfs();
4,375,334✔
824

825
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
4,375,334✔
826
    atomic_store_64(&pTaskInfo->owner, 0);
4,375,334✔
827

828
    return pTaskInfo->code;
4,375,334✔
829
  }
830

831
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
288,695,249✔
832

833
  int32_t      current = 0;
288,696,210✔
834
  SSDataBlock* pRes = NULL;
288,696,210✔
835
  int64_t      st = taosGetTimestampUs();
289,061,588✔
836

837
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
289,061,588✔
838
    pTaskInfo->paramSet = true;
7,881,371✔
839
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
7,881,371✔
840
  } else {
841
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
281,199,979✔
842
  }
843

844
  QUERY_CHECK_CODE(code, lino, _end);
284,700,099✔
845
  code = blockDataCheck(pRes);
284,700,099✔
846
  QUERY_CHECK_CODE(code, lino, _end);
284,666,600✔
847

848
  if (pRes == NULL) {
284,666,600✔
849
    st = taosGetTimestampUs();
71,044,534✔
850
  }
851

852
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
284,675,746✔
853
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
284,738,070✔
854
    rowsThreshold = 4096;
283,979,015✔
855
  }
856

857
  int32_t blockIndex = 0;
284,717,449✔
858
  while (pRes != NULL) {
736,300,849✔
859
    SSDataBlock* p = NULL;
469,852,497✔
860
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
469,716,291✔
861
      SSDataBlock* p1 = NULL;
360,568,387✔
862
      code = createOneDataBlock(pRes, true, &p1);
360,585,336✔
863
      QUERY_CHECK_CODE(code, lino, _end);
360,519,016✔
864

865
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
360,519,016✔
866
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
360,621,644✔
867
      p = p1;
360,621,644✔
868
    } else {
869
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
109,261,759✔
870
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
109,261,168✔
871

872
      p = *(SSDataBlock**)tmp;
109,261,168✔
873
      code = copyDataBlock(p, pRes);
109,261,132✔
874
      QUERY_CHECK_CODE(code, lino, _end);
109,260,541✔
875
    }
876

877
    blockIndex += 1;
469,857,755✔
878

879
    current += p->info.rows;
469,857,755✔
880
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
469,876,897✔
881
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
882
    void* tmp = taosArrayPush(pResList, &p);
469,846,471✔
883
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
469,846,471✔
884

885
    if (current >= rowsThreshold || processOneBlock) {
469,846,471✔
886
      break;
887
    }
888

889
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
451,571,636✔
890
    QUERY_CHECK_CODE(code, lino, _end);
451,618,164✔
891
    code = blockDataCheck(pRes);
451,618,164✔
892
    QUERY_CHECK_CODE(code, lino, _end);
451,638,573✔
893
  }
894

895
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
284,723,187✔
896
    pTaskInfo->pSubplan->rowsThreshold -= current;
724,933✔
897
  }
898

899
  *hasMore = (pRes != NULL);
284,698,134✔
900
  uint64_t el = (taosGetTimestampUs() - st);
284,587,532✔
901

902
  pTaskInfo->cost.elapsedTime += el;
284,587,532✔
903
  if (NULL == pRes) {
284,525,827✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
266,250,992✔
905
  }
906

907
_end:
284,431,310✔
908
  (void)cleanUpUdfs();
284,479,294✔
909

910
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
284,836,530✔
911
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
284,835,987✔
912
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
913

914
  atomic_store_64(&pTaskInfo->owner, 0);
284,836,405✔
915
  if (code) {
284,834,867✔
916
    pTaskInfo->code = code;
×
917
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
918
  }
919

920
  return pTaskInfo->code;
284,834,867✔
921
}
922

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

934
  taosArrayClear(pTaskInfo->pResultBlockList);
×
935
}
×
936

937
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
47,364,879✔
938
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,364,879✔
939
  int64_t        threadId = taosGetSelfPthreadId();
47,364,879✔
940
  int64_t        curOwner = 0;
47,365,323✔
941

942
  *pRes = NULL;
47,365,323✔
943

944
  // todo extract method
945
  taosRLockLatch(&pTaskInfo->lock);
47,366,196✔
946
  bool isKilled = isTaskKilled(pTaskInfo);
47,366,418✔
947
  if (isKilled) {
47,366,418✔
948
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
949

950
    taosRUnLockLatch(&pTaskInfo->lock);
×
951
    return pTaskInfo->code;
×
952
  }
953

954
  if (pTaskInfo->owner != 0) {
47,366,418✔
955
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
956
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
957

958
    taosRUnLockLatch(&pTaskInfo->lock);
×
959
    return pTaskInfo->code;
×
960
  }
961

962
  pTaskInfo->owner = threadId;
47,366,339✔
963
  taosRUnLockLatch(&pTaskInfo->lock);
47,366,418✔
964

965
  if (pTaskInfo->cost.start == 0) {
47,366,307✔
966
    pTaskInfo->cost.start = taosGetTimestampUs();
94,550✔
967
  }
968

969
  // error occurs, record the error code and return to client
970
  int32_t ret = setjmp(pTaskInfo->env);
47,366,307✔
971
  if (ret != TSDB_CODE_SUCCESS) {
47,363,953✔
972
    pTaskInfo->code = ret;
×
973
    (void)cleanUpUdfs();
×
974
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
975
    atomic_store_64(&pTaskInfo->owner, 0);
×
976
    return pTaskInfo->code;
×
977
  }
978

979
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
47,363,953✔
980

981
  int64_t st = taosGetTimestampUs();
47,364,987✔
982
  int32_t code = TSDB_CODE_SUCCESS;
47,364,987✔
983
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
47,364,987✔
984
    pTaskInfo->paramSet = true;
×
985
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
986
  } else {
987
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
47,365,419✔
988
  }
989
  if (code) {
47,337,410✔
990
    pTaskInfo->code = code;
×
991
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
992
  }
993

994
  code = blockDataCheck(*pRes);
47,337,410✔
995
  if (code) {
47,364,590✔
996
    pTaskInfo->code = code;
×
997
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
998
  }
999

1000
  uint64_t el = (taosGetTimestampUs() - st);
47,355,356✔
1001

1002
  pTaskInfo->cost.elapsedTime += el;
47,355,356✔
1003
  if (NULL == *pRes) {
47,358,848✔
1004
    *useconds = pTaskInfo->cost.elapsedTime;
4,492,521✔
1005
  }
1006

1007
  (void)cleanUpUdfs();
47,358,178✔
1008

1009
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
47,366,418✔
1010
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
47,366,418✔
1011

1012
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
47,365,656✔
1013
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1014

1015
  atomic_store_64(&pTaskInfo->owner, 0);
47,366,085✔
1016
  return pTaskInfo->code;
47,366,418✔
1017
}
1018

1019
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
66,898,877✔
1020
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
66,898,877✔
1021
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
66,900,091✔
1022
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
66,899,446✔
1023

1024
  if (!tmp) {
66,898,843✔
1025
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1026
    return terrno;
×
1027
  }
1028
  return TSDB_CODE_SUCCESS;
66,898,843✔
1029
}
1030

1031
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1032
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1033
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1034

1035
  if (key->refId < pInfo->refId) {
×
1036
    return -1;
×
1037
  } else if (key->refId > pInfo->refId) {
×
1038
    return 1;
×
1039
  }
1040

1041
  return 0;
×
1042
}
1043

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

1053
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
58,925✔
1054
  if (pTaskInfo->subJobCtx.hasSubJobs) {
58,925✔
1055
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
21,016✔
1056
    if (pTaskInfo->subJobCtx.param) {
21,016✔
1057
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
7,235✔
1058
    }
1059
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
21,016✔
1060
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
21,016✔
1061
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
21,016✔
1062
  }
1063
  
1064
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
58,925✔
1065

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

1088
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
58,925✔
1089
}
58,925✔
1090

1091
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
58,925✔
1092
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
58,925✔
1093
  if (pTaskInfo == NULL) {
58,925✔
1094
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1095
  }
1096

1097
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
58,925✔
1098

1099
  setTaskKilled(pTaskInfo, rspCode);
58,925✔
1100
  qStopTaskOperators(pTaskInfo);
58,925✔
1101

1102
  return TSDB_CODE_SUCCESS;
58,925✔
1103
}
1104

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

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

1118
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1119

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

1126
        taosMsleep(200);
×
1127

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

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

1149
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1150
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1151
  if (NULL == pTaskInfo) {
×
1152
    return false;
×
1153
  }
1154

1155
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1156
}
1157

1158
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
266,217,474✔
1159
  STaskCostInfo* pSummary = &pTaskInfo->cost;
266,217,474✔
1160
  int64_t        idleTime = pSummary->start - pSummary->created;
266,253,151✔
1161

1162
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
266,266,839✔
1163
  if (pSummary->pRecoder != NULL) {
266,110,947✔
1164
    qDebug(
207,402,491✔
1165
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1166
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1167
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1168
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1169
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1170
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1171
  } else {
1172
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
58,749,656✔
1173
           pSummary->elapsedTime / 1000.0);
1174
  }
1175
}
266,152,147✔
1176

1177

1178
void qDestroyTask(qTaskInfo_t qTaskHandle) {
278,633,367✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
278,633,367✔
1180
  if (pTaskInfo == NULL) {
278,633,367✔
1181
    return;
12,443,420✔
1182
  }
1183

1184
  if (pTaskInfo->pRoot != NULL) {
266,189,947✔
1185
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
266,202,455✔
1186
  } else {
1187
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1188
  }
1189

1190
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
266,203,860✔
1191
  doDestroyTask(pTaskInfo);
266,199,801✔
1192
}
1193

1194
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,717,196✔
1195
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,717,196✔
1196
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,717,196✔
1197
}
1198

1199
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
117,056✔
1200
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
117,056✔
1201
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
117,056✔
1202

1203
  while (1) {
115,998✔
1204
    uint16_t type = pOperator->operatorType;
233,054✔
1205
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
233,054✔
1206
      *scanner = pOperator->info;
117,056✔
1207
      break;
117,056✔
1208
    } else {
1209
      pOperator = pOperator->pDownstream[0];
115,998✔
1210
    }
1211
  }
1212
}
117,056✔
1213

1214
void* qExtractReaderFromTmqScanner(void* scanner) {
117,056✔
1215
  SStreamScanInfo* pInfo = scanner;
117,056✔
1216
  return (void*)pInfo->tqReader;
117,056✔
1217
}
1218

1219
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
145,878✔
1220
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
145,878✔
1221
  return pTaskInfo->streamInfo.schema;
145,878✔
1222
}
1223

1224
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
145,878✔
1225
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
145,878✔
1226
  return pTaskInfo->streamInfo.tbName;
145,878✔
1227
}
1228

1229
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
162,472✔
1230
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
162,472✔
1231
  return &pTaskInfo->streamInfo.btMetaRsp;
162,472✔
1232
}
1233

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

1245
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
153,212✔
1246
  memset(pCond, 0, sizeof(SQueryTableDataCond));
153,212✔
1247
  pCond->order = TSDB_ORDER_ASC;
153,212✔
1248
  pCond->numOfCols = pMtInfo->schema->nCols;
153,497✔
1249
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
153,611✔
1250
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
153,312✔
1251
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
153,554✔
1252
    taosMemoryFreeClear(pCond->colList);
114✔
1253
    taosMemoryFreeClear(pCond->pSlotList);
×
1254
    return terrno;
×
1255
  }
1256

1257
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
153,326✔
1258
  pCond->suid = pMtInfo->suid;
153,483✔
1259
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
153,483✔
1260
  pCond->startVersion = -1;
153,483✔
1261
  pCond->endVersion = sContext->snapVersion;
153,269✔
1262

1263
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
986,503✔
1264
    SColumnInfo* pColInfo = &pCond->colList[i];
832,977✔
1265
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
832,906✔
1266
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
833,177✔
1267
    if (pMtInfo->pExtSchemas != NULL) {
833,020✔
1268
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
7,818✔
1269
    }
1270
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
833,262✔
1271
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
833,063✔
1272

1273
    pCond->pSlotList[i] = i;
833,262✔
1274
  }
1275

1276
  return TSDB_CODE_SUCCESS;
153,668✔
1277
}
1278

1279
void qStreamSetOpen(qTaskInfo_t tinfo) {
47,060,449✔
1280
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
47,060,449✔
1281
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
47,060,449✔
1282
  pOperator->status = OP_NOT_OPENED;
47,064,168✔
1283
}
47,063,635✔
1284

1285
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
4,757,449✔
1286
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,757,449✔
1287
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
4,757,449✔
1288
}
4,758,384✔
1289

1290
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
4,970,044✔
1291
  int32_t        code = TSDB_CODE_SUCCESS;
4,970,044✔
1292
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,970,044✔
1293
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
4,970,044✔
1294

1295
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
4,970,625✔
1296
  const char*    id = GET_TASKID(pTaskInfo);
4,970,515✔
1297

1298
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
4,970,241✔
1299
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
4,699,701✔
1300
    if (pOperator == NULL || code != 0) {
4,699,071✔
1301
      return code;
76✔
1302
    }
1303

1304
    SStreamScanInfo* pInfo = pOperator->info;
4,699,016✔
1305
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
4,699,120✔
1306
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
4,698,881✔
1307
    walReaderVerifyOffset(pWalReader, pOffset);
4,699,353✔
1308
  }
1309
  // if pOffset equal to current offset, means continue consume
1310
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
4,970,181✔
1311
    return 0;
4,525,408✔
1312
  }
1313

1314
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
442,828✔
1315
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
285,178✔
1316
    if (pOperator == NULL || code != 0) {
285,095✔
1317
      return code;
×
1318
    }
1319

1320
    SStreamScanInfo* pInfo = pOperator->info;
285,178✔
1321
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
285,068✔
1322
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
285,095✔
1323
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
285,095✔
1324

1325
    if (pOffset->type == TMQ_OFFSET__LOG) {
285,031✔
1326
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
237,231✔
1327
      pScanBaseInfo->dataReader = NULL;
237,316✔
1328

1329
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
237,316✔
1330
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
237,316✔
1331
      walReaderVerifyOffset(pWalReader, pOffset);
237,206✔
1332
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
237,341✔
1333
      if (code < 0) {
237,341✔
1334
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
7,863✔
1335
        return code;
7,863✔
1336
      }
1337
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
47,516✔
1338
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1339
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1340
      int64_t uid = pOffset->uid;
47,837✔
1341
      int64_t ts = pOffset->ts;
47,837✔
1342
      int32_t index = 0;
47,588✔
1343

1344
      // this value may be changed if new tables are created
1345
      taosRLockLatch(&pTaskInfo->lock);
47,588✔
1346
      int32_t numOfTables = 0;
47,837✔
1347
      code = tableListGetSize(pTableListInfo, &numOfTables);
47,837✔
1348
      if (code != TSDB_CODE_SUCCESS) {
47,515✔
1349
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1350
        taosRUnLockLatch(&pTaskInfo->lock);
×
1351
        return code;
×
1352
      }
1353

1354
      if (uid == 0) {
47,515✔
1355
        if (numOfTables != 0) {
46,332✔
1356
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
8,398✔
1357
          if (!tmp) {
8,370✔
1358
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1359
            taosRUnLockLatch(&pTaskInfo->lock);
×
1360
            return terrno;
×
1361
          }
1362
          if (tmp) uid = tmp->uid;
8,370✔
1363
          ts = INT64_MIN;
8,370✔
1364
          pScanInfo->currentTable = 0;
8,370✔
1365
        } else {
1366
          taosRUnLockLatch(&pTaskInfo->lock);
37,934✔
1367
          qError("no table in table list, %s", id);
37,934✔
1368
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
38,017✔
1369
        }
1370
      }
1371
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
9,553✔
1372

1373
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
9,820✔
1374
             pInfo->pTableScanOp->resultInfo.totalRows);
1375
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
9,820✔
1376

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

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

1391
      STableKeyInfo keyInfo = {.uid = uid};
9,820✔
1392
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
9,820✔
1393

1394
      // let's start from the next ts that returned to consumer.
1395
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
9,820✔
1396
        pScanBaseInfo->cond.twindows.skey = ts;
229✔
1397
      } else {
1398
        pScanBaseInfo->cond.twindows.skey = ts + 1;
9,591✔
1399
      }
1400
      pScanInfo->scanTimes = 0;
9,820✔
1401

1402
      if (pScanBaseInfo->dataReader == NULL) {
9,820✔
1403
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
16,684✔
1404
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1405
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
8,342✔
1406
        if (code != TSDB_CODE_SUCCESS) {
8,286✔
1407
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1408
          return code;
×
1409
        }
1410

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

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

1429
      // restore the key value
1430
      pScanBaseInfo->cond.twindows.skey = oldSkey;
9,792✔
1431
    } else {
1432
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1433
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1434
    }
1435

1436
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1437
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
157,650✔
1438
      SStreamRawScanInfo* pInfo = pOperator->info;
153,858✔
1439
      SSnapContext*       sContext = pInfo->sContext;
153,858✔
1440
      SOperatorInfo*      p = NULL;
153,858✔
1441

1442
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
153,858✔
1443
      if (code != 0) {
153,858✔
1444
        return code;
×
1445
      }
1446

1447
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
153,858✔
1448

1449
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
153,801✔
1450
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1451
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1452
      }
1453

1454
      SMetaTableInfo mtInfo = {0};
153,858✔
1455
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
153,858✔
1456
      if (code != 0) {
153,858✔
1457
        destroyMetaTableInfo(&mtInfo);
1458
        return code;
×
1459
      }
1460
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
153,858✔
1461
      pInfo->dataReader = NULL;
153,687✔
1462

1463
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
153,687✔
1464
      tableListClear(pTableListInfo);
153,559✔
1465

1466
      if (mtInfo.uid == 0) {
153,730✔
1467
        destroyMetaTableInfo(&mtInfo);
1468
        goto end;  // no data
190✔
1469
      }
1470

1471
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
153,540✔
1472
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
153,383✔
1473
      if (code != TSDB_CODE_SUCCESS) {
153,483✔
1474
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1475
        destroyMetaTableInfo(&mtInfo);
1476
        return code;
×
1477
      }
1478
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
153,483✔
1479
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
387✔
1480
      } else {
1481
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
152,868✔
1482
      }
1483

1484
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
153,483✔
1485
      if (code != TSDB_CODE_SUCCESS) {
153,668✔
1486
        destroyMetaTableInfo(&mtInfo);
1487
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1488
        return code;
×
1489
      }
1490

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

1505
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
307,336✔
1506
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
153,668✔
1507
      if (code != TSDB_CODE_SUCCESS) {
153,369✔
1508
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1509
        destroyMetaTableInfo(&mtInfo);
1510
        return code;
×
1511
      }
1512

1513
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
153,369✔
1514
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
153,540✔
1515
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1516
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
153,198✔
1517
      pTaskInfo->streamInfo.schema = mtInfo.schema;
153,341✔
1518
      taosMemoryFreeClear(mtInfo.pExtSchemas);
153,341✔
1519

1520
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
153,341✔
1521
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
3,792✔
1522
      SStreamRawScanInfo* pInfo = pOperator->info;
1,260✔
1523
      SSnapContext*       sContext = pInfo->sContext;
1,260✔
1524
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
1,260✔
1525
      if (code != 0) {
1,260✔
1526
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1527
        return code;
×
1528
      }
1529
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
1,260✔
1530
             id);
1531
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
2,532✔
1532
      SStreamRawScanInfo* pInfo = pOperator->info;
2,532✔
1533
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,532✔
1534
      pInfo->dataReader = NULL;
2,532✔
1535
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
2,532✔
1536
    }
1537
  }
1538

1539
end:
396,942✔
1540
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
396,948✔
1541
  return 0;
396,948✔
1542
}
1543

1544
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
182,697,981✔
1545
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
182,697,981✔
1546
  if (pMsg->info.ahandle == NULL) {
182,718,952✔
1547
    rpcFreeCont(pMsg->pCont);
×
1548
    qError("pMsg->info.ahandle is NULL");
×
1549
    return;
×
1550
  }
1551

1552
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
182,686,731✔
1553
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1554

1555
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
182,693,018✔
1556

1557
  if (pMsg->contLen > 0) {
182,701,314✔
1558
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
182,665,068✔
1559
    if (buf.pData == NULL) {
182,595,311✔
1560
      pMsg->code = terrno;
×
1561
    } else {
1562
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
182,595,311✔
1563
    }
1564
  }
1565

1566
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
182,680,313✔
1567
  rpcFreeCont(pMsg->pCont);
182,731,151✔
1568
  destroySendMsgInfo(pSendInfo);
182,685,021✔
1569
}
1570

1571
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1572
  int32_t        code = TSDB_CODE_SUCCESS;
×
1573
  int32_t        lino = 0;
×
1574
  SExecTaskInfo* pTaskInfo = tinfo;
×
1575
  SArray*        plist = NULL;
×
1576

1577
  code = getTableListInfo(pTaskInfo, &plist);
×
1578
  if (code || plist == NULL) {
×
1579
    return NULL;
×
1580
  }
1581

1582
  // only extract table in the first elements
1583
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1584

1585
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1586
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1587

1588
  int32_t numOfTables = 0;
×
1589
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1590
  QUERY_CHECK_CODE(code, lino, _end);
×
1591

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

1599
  taosArrayDestroy(plist);
×
1600

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

1609
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,551,962✔
1610
  int32_t        code = TSDB_CODE_SUCCESS;
3,551,962✔
1611
  int32_t        lino = 0;
3,551,962✔
1612
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,551,962✔
1613

1614
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,551,962✔
1615
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1616
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1617

1618
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1619
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1620
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,548,212✔
1621
    STableScanInfo* pScanInfo = pOperator->info;
1,775,356✔
1622

1623
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,775,356✔
1624
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,776,606✔
1625
  } else {
1626
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,776,606✔
1627
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,776,606✔
1628
    }
1629
  }
1630

1631
_end:
×
1632
  if (code != TSDB_CODE_SUCCESS) {
3,551,337✔
1633
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1634
  }
1635
  return code;
3,550,087✔
1636
}
1637

1638
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,776,606✔
1639
  if (pList == NULL) {
1,776,606✔
1640
    return TSDB_CODE_INVALID_PARA;
×
1641
  }
1642

1643
  *pList = NULL;
1,776,606✔
1644
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,776,606✔
1645
  if (pArray == NULL) {
1,775,981✔
1646
    return terrno;
×
1647
  }
1648

1649
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,775,981✔
1650
  if (code == 0) {
1,774,731✔
1651
    *pList = pArray;
1,774,731✔
1652
  } else {
1653
    taosArrayDestroy(pArray);
×
1654
  }
1655
  return code;
1,774,731✔
1656
}
1657

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

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

1674
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1675
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1676

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

1682
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1683
  int32_t code = 0;
×
1684
  return code;
×
1685
}
1686

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

1693
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
35,900,033✔
1694
  int32_t code = 0;
35,900,033✔
1695

1696
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
35,900,033✔
1697
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
35,899,266✔
1698

1699
  if (pOper->fpSet.resetStateFn) {
35,900,894✔
1700
    code = pOper->fpSet.resetStateFn(pOper);
35,901,301✔
1701
  }
1702
  pOper->status = OP_NOT_OPENED;
35,893,520✔
1703
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
60,999,661✔
1704
    code = clearStatesForOperator(pOper->pDownstream[i]);
25,099,593✔
1705
  }
1706
  return code;
35,902,516✔
1707
}
1708

1709
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
10,801,678✔
1710
  int32_t        code = 0;
10,801,678✔
1711
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,801,678✔
1712
  SOperatorInfo* pOper = pTaskInfo->pRoot;
10,801,678✔
1713
  pTaskInfo->code = TSDB_CODE_SUCCESS;
10,801,678✔
1714
  code = clearStatesForOperator(pOper);
10,801,265✔
1715
  return code;
10,801,678✔
1716
}
1717

1718
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
14,172,608✔
1719
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
14,172,608✔
1720
  int64_t        threadId = taosGetSelfPthreadId();
14,172,608✔
1721
  int64_t        curOwner = 0;
14,172,608✔
1722

1723
  *ppRes = NULL;
14,172,608✔
1724

1725
  // todo extract method
1726
  taosRLockLatch(&pTaskInfo->lock);
14,172,608✔
1727
  bool isKilled = isTaskKilled(pTaskInfo);
14,173,019✔
1728
  if (isKilled) {
14,173,019✔
1729
    // clearStreamBlock(pTaskInfo->pRoot);
1730
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1731

1732
    taosRUnLockLatch(&pTaskInfo->lock);
×
1733
    return pTaskInfo->code;
×
1734
  }
1735

1736
  if (pTaskInfo->owner != 0) {
14,173,019✔
1737
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1738
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1739

1740
    taosRUnLockLatch(&pTaskInfo->lock);
×
1741
    return pTaskInfo->code;
×
1742
  }
1743

1744
  pTaskInfo->owner = threadId;
14,173,019✔
1745
  taosRUnLockLatch(&pTaskInfo->lock);
14,173,019✔
1746

1747
  if (pTaskInfo->cost.start == 0) {
14,173,019✔
1748
    pTaskInfo->cost.start = taosGetTimestampUs();
340,955✔
1749
  }
1750

1751
  // error occurs, record the error code and return to client
1752
  int32_t ret = setjmp(pTaskInfo->env);
14,173,019✔
1753
  if (ret != TSDB_CODE_SUCCESS) {
16,436,488✔
1754
    pTaskInfo->code = ret;
2,264,292✔
1755
    (void)cleanUpUdfs();
2,264,292✔
1756
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,264,292✔
1757
    atomic_store_64(&pTaskInfo->owner, 0);
2,264,292✔
1758
    return pTaskInfo->code;
2,264,292✔
1759
  }
1760

1761
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
14,172,196✔
1762

1763
  int64_t st = taosGetTimestampUs();
14,172,196✔
1764

1765
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
14,172,196✔
1766
  if (code) {
11,908,727✔
1767
    pTaskInfo->code = code;
×
1768
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1769
  } else {
1770
    *finished = *ppRes == NULL;
11,908,727✔
1771
    code = blockDataCheck(*ppRes);
11,908,727✔
1772
  }
1773
  if (code) {
11,908,290✔
1774
    pTaskInfo->code = code;
×
1775
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1776
  }
1777

1778
  uint64_t el = (taosGetTimestampUs() - st);
11,907,859✔
1779

1780
  pTaskInfo->cost.elapsedTime += el;
11,907,859✔
1781
  if (NULL == *ppRes) {
11,907,859✔
1782
    *useconds = pTaskInfo->cost.elapsedTime;
8,046,336✔
1783
  }
1784

1785
  (void)cleanUpUdfs();
11,907,859✔
1786

1787
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
11,908,727✔
1788
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
11,908,727✔
1789

1790
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
11,908,727✔
1791
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1792

1793
  atomic_store_64(&pTaskInfo->owner, 0);
11,908,727✔
1794
  return pTaskInfo->code;
11,908,727✔
1795
}
1796

1797
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1798
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1799
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1800
// }
1801

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

1817
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
333,042✔
1818
  SReadHandle    pHandle = {.vnode = pVnode};
333,042✔
1819
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
333,042✔
1820

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

1829
end:
333,042✔
1830
  return 0;
333,042✔
1831
}
1832

1833
static int32_t doFilterTableByTagCond(void* pVnode, void* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
62,570✔
1834
  bool   listAdded = false;
62,570✔
1835
  int32_t code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, true, &listAdded, NULL);
62,570✔
1836
  if (code == 0 && !listAdded) {
62,570✔
1837
    int32_t numOfTables = taosArrayGetSize(pUidList);
53,013✔
1838
    for (int i = 0; i < numOfTables; i++) {
106,026✔
1839
      void* tmp = taosArrayGet(pUidList, i);
53,013✔
1840
      if (tmp == NULL) {
53,013✔
1841
        return terrno;
×
1842
      }
1843
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
53,013✔
1844

1845
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
53,013✔
1846
      if (p == NULL) {
53,013✔
1847
        return terrno;
×
1848
      }
1849
    }
1850
  }
1851
  return code;
62,570✔
1852
}
1853

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

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

1884
  taosArrayClear(uidList);
61,374✔
1885
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
122,748✔
1886
    void* tmp = taosArrayGet(uidListCopy, i);
60,961✔
1887
    if (tmp == NULL) {
60,961✔
1888
      continue;
×
1889
    }
1890
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
60,961✔
1891
    if (slot == NULL) {
61,374✔
1892
      if (taosArrayPush(uidList, tmp) == NULL) {
6,253✔
1893
        code = terrno;
×
1894
        goto end;
×
1895
      }
1896
    }
1897
  }
1898
end:
61,374✔
1899
  taosArrayDestroy(uidListCopy);
61,374✔
1900
  tableListDestroy(pList);
61,374✔
1901
  return code;
61,374✔
1902
}
1903

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

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

1925
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
333,042✔
1926
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
333,042✔
1927
  STableListInfo* pList = pTableListInfo;
333,042✔
1928
  return pList->pTableList;
333,042✔
1929
}
1930

1931
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
2,043,319✔
1932

1933
void streamDestroyExecTask(qTaskInfo_t tInfo) {
3,371,265✔
1934
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
3,371,265✔
1935
  qDestroyTask(tInfo);
3,371,265✔
1936
}
3,371,265✔
1937

1938
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
874,502✔
1939
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
874,502✔
1940
}
1941

1942
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
898,152✔
1943
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1944
  int32_t      code = 0;
898,152✔
1945
  SNode*       pNode = 0;
898,152✔
1946
  SNodeList*   pList = NULL;
898,152✔
1947
  SExprInfo*   pExprInfo = NULL;
897,742✔
1948
  int32_t      numOfExprs = 1;
897,742✔
1949
  int32_t*     offset = 0;
897,742✔
1950
  STargetNode* pTargetNode = NULL;
897,742✔
1951
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
897,742✔
1952
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
898,152✔
1953

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

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

2006
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
4,204,506✔
2007
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
4,204,506✔
2008
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
4,204,506✔
2009
  int32_t        code = 0;
4,204,506✔
2010
  SNode*         pNode = NULL;
4,204,506✔
2011
  if (!pForceOutputCols) return 0;
4,204,506✔
2012
  if (!*pRes) {
23,240✔
2013
    code = createDataBlock(pRes);
23,240✔
2014
  }
2015

2016
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
23,240✔
2017
    int32_t idx = 0;
23,240✔
2018
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
92,572✔
2019
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
69,332✔
2020
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
69,332✔
2021
      colInfo.info.precision = pCol->type.precision;
69,332✔
2022
      colInfo.info.scale = pCol->type.scale;
69,332✔
2023
      code = blockDataAppendColInfo(*pRes, &colInfo);
69,332✔
2024
      if (code != 0) break;
69,332✔
2025
    }
2026
  }
2027

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

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

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

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

2146
    memcpy(tbname, pVal, len);
339,418✔
2147
    tbname[len] = '\0';  // ensure null terminated
339,418✔
2148
    if (NULL != strchr(tbname, '.')) {
339,057✔
2149
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2150
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2151
    }
2152
  }
2153
  // TODO free dst
2154
  sclFreeParam(&dst);
344,020✔
2155
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
344,833✔
2156
  return code;
344,833✔
2157
}
2158

2159
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
332,053✔
2160
  if (pParam) {
332,053✔
2161
    if (pParam->tbname) {
332,053✔
2162
      taosMemFree(pParam->tbname);
332,053✔
2163
      pParam->tbname = NULL;
332,053✔
2164
    }
2165
    if (pParam->stbname) {
332,053✔
2166
      taosMemFree(pParam->stbname);
332,053✔
2167
      pParam->stbname = NULL;
332,053✔
2168
    }
2169
    if (pParam->dbFName) {
332,053✔
2170
      taosMemFree(pParam->dbFName);
332,053✔
2171
      pParam->dbFName = NULL;
332,053✔
2172
    }
2173
    if (pParam->pFields) {
332,053✔
2174
      taosArrayDestroy(pParam->pFields);
332,053✔
2175
      pParam->pFields = NULL;
332,053✔
2176
    }
2177
    if (pParam->pTagFields) {
332,053✔
2178
      taosArrayDestroy(pParam->pTagFields);
221,650✔
2179
      pParam->pTagFields = NULL;
221,650✔
2180
    }
2181
    taosMemFree(pParam);
332,053✔
2182
  }
2183
}
332,053✔
2184

2185
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
332,053✔
2186
  int32_t code = 0, lino = 0;
332,053✔
2187
  if (ppDst == NULL || pSrc == NULL) {
332,053✔
2188
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2189
  }
2190
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
332,053✔
2191
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
332,053✔
2192

2193
  (*ppDst)->suid = pSrc->suid;
332,053✔
2194
  (*ppDst)->sver = pSrc->sver;
332,053✔
2195
  (*ppDst)->tbType = pSrc->tbType;
332,053✔
2196
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
332,053✔
2197
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
332,053✔
2198

2199
  if (pSrc->stbname) {
332,053✔
2200
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
331,692✔
2201
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
332,053✔
2202
  }
2203

2204
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
332,414✔
2205
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
332,053✔
2206

2207
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
331,692✔
2208

2209
  if (pSrc->pFields && pSrc->pFields->size > 0) {
332,053✔
2210
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
332,053✔
2211
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
332,053✔
2212
  } else {
2213
    (*ppDst)->pFields = NULL;
×
2214
  }
2215
  
2216
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
331,692✔
2217
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
221,650✔
2218
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
221,650✔
2219
  } else {
2220
    (*ppDst)->pTagFields = NULL;
110,042✔
2221
  }
2222

2223
_exit:
332,053✔
2224

2225
  if (code != 0) {
332,053✔
2226
    if (*ppDst) {
×
2227
      destroyStreamInserterParam(*ppDst);
×
2228
      *ppDst = NULL;
×
2229
    }
2230
    
2231
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2232
  }
2233
  return code;
332,053✔
2234
}
2235

2236
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
442✔
2237
  return doDropStreamTable(pMsgCb, pOutput, pReq);
442✔
2238
}
2239

2240
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2241
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2242
}
2243

2244
int32_t qSubFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
1,196✔
2245
  int32_t code = TSDB_CODE_SUCCESS;
1,196✔
2246
  STableListInfo* pList = tableListCreate();
1,196✔
2247
  if (pList == NULL) {
1,196✔
2248
    code = terrno;
×
2249
    goto end;
×
2250
  }
2251
  // SArray* uidListCopy = taosArrayDup(uidList, NULL);
2252
  // if (uidListCopy == NULL) {
2253
  //   code = terrno;
2254
  //   goto end;
2255
  // }
2256

2257
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
1,196✔
2258
  pList->idInfo.suid = suid;
1,196✔
2259
  pList->idInfo.tableType = TD_SUPER_TABLE;
1,196✔
2260
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
1,196✔
2261
  if (code != TSDB_CODE_SUCCESS) {
1,196✔
2262
    goto end;
×
2263
  }                                              
2264
  // taosArrayClear(uidList);
2265
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2266
  //   void* tmp = taosArrayGet(uidListCopy, i);
2267
  //   if (tmp == NULL) {
2268
  //     continue;
2269
  //   }
2270
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2271
  //   if (slot == NULL) {
2272
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2273
  //       code = terrno;
2274
  //       goto end;
2275
  //     }
2276
  //   }
2277
  // }
2278
end:
1,196✔
2279
  // taosArrayDestroy(uidListCopy);
2280
  tableListDestroy(pList);
1,196✔
2281
  return code;
1,196✔
2282
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc