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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

69.62
/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) {
710,671,593✔
44
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
710,671,593✔
45
  gTaskScalarExtra.pSubJobCtx = &pTaskInfo->subJobCtx;
710,671,593✔
46
  gTaskScalarExtra.fp = qFetchRemoteValue;
710,926,353✔
47
}
710,727,006✔
48

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

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

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

70
static void initRefPool() {
522,715✔
71
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
522,715✔
72
  (void)atexit(cleanupRefPool);
522,715✔
73
}
522,715✔
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) {
42,592,188✔
157
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
42,592,188✔
158
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
42,593,677✔
159
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
19,483,646✔
160
    if (pStreamScanInfo->pTableScanOp != NULL) {
19,483,948✔
161
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
19,483,948✔
162
      if (pScanInfo->base.dataReader != NULL) {
19,483,626✔
163
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
254,029✔
164
        if (code) {
254,029✔
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);
23,108,783✔
172
  }
173

174
  return 0;
22,629,725✔
175
}
176

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

182
  // set the idstr for tsdbReader
183
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
22,630,337✔
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) {
409,678✔
213
  if (msg == NULL) {  // create raw scan
409,678✔
214
    SExecTaskInfo* pTaskInfo = NULL;
106,203✔
215

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

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

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

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

248
  return pTaskInfo;
306,904✔
249
}
250

251
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
498,372✔
252
  if (streamInserterParam == NULL) {
498,372✔
253
    return TSDB_CODE_SUCCESS;
296,426✔
254
  }
255

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

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

272
  return TSDB_CODE_SUCCESS;
201,946✔
273
}
274

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

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

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

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

322
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
201,946✔
323
    if (code) {
201,946✔
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,
498,166✔
328
         tstrerror(code));
329

330
_error:
69,372✔
331

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

341
bool qNeedReset(qTaskInfo_t pInfo) {
2,648,291✔
342
  if (pInfo == NULL) {
2,648,291✔
343
    return false;
×
344
  }
345
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
2,648,291✔
346
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
2,648,291✔
347
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
2,648,497✔
UNCOV
348
    return false;
×
349
  }
350
  int32_t node = nodeType(pOperator->pPhyNode);
2,648,497✔
351
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,431,492✔
352
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
416,012✔
353
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
5,079,989✔
354
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
355
}
356

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

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

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

377
  void*           info = pOperator->info;
2,648,497✔
378
  STableScanBase* pScanBaseInfo = NULL;
2,648,497✔
379

380
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
2,648,497✔
381
    pScanBaseInfo = &((STableScanInfo*)info)->base;
217,005✔
382
    setReadHandle(handle, pScanBaseInfo);
217,005✔
383
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,431,492✔
384
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,015,480✔
385
    setReadHandle(handle, pScanBaseInfo);
2,015,480✔
386
  }
387

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

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

399
  *pTaskInfo = NULL;
498,372✔
400

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

416
  return code;
498,166✔
417
}
418

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

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

432
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
338,548✔
433

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

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

443
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
338,548✔
444

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

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

454
  locked = 1;
338,548✔
455
  for (int32_t i = 0; i < numOfUids; ++i) {
683,846✔
456
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
345,298✔
457
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
345,298✔
458

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

465
    tDecoderClear(&mr.coder);
345,298✔
466

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

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

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

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

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

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

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

521
  // handle multiple partition
522

523
_end:
338,548✔
524

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

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

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

543
  if (isAdd) {
340,491✔
544
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
338,548✔
545
  }
546

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

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

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

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

586
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
338,548✔
587
    taosWLockLatch(&pTaskInfo->lock);
338,548✔
588

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

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

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

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

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

636
  return code;
340,491✔
637
}
638

639
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
606,979,725✔
640
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
641
                                    int32_t idx, bool* tbGet) {
642
  *tbGet = false;
606,979,725✔
643

644
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
607,385,053✔
645
    return TSDB_CODE_INVALID_PARA;
32,341✔
646
  }
647
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
607,353,083✔
648

649
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
607,353,083✔
650
    return TSDB_CODE_SUCCESS;
343,167,377✔
651
  }
652

653
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
264,414,303✔
654
  if (!pSchemaInfo) {
264,181,058✔
655
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
656
    return terrno;
×
657
  }
658

659
  *sversion = pSchemaInfo->sw->version;
264,181,058✔
660
  *tversion = pSchemaInfo->tversion;
264,515,511✔
661
  *rversion = pSchemaInfo->rversion;
264,398,842✔
662
  if (pSchemaInfo->dbname) {
264,293,590✔
663
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
264,268,306✔
664
  } else {
665
    dbName[0] = 0;
×
666
  }
667
  if (pSchemaInfo->tablename) {
264,480,675✔
668
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
264,196,866✔
669
  } else {
670
    tableName[0] = 0;
88,733✔
671
  }
672

673
  *tbGet = true;
264,631,006✔
674

675
  return TSDB_CODE_SUCCESS;
264,544,300✔
676
}
677

678
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
342,926,197✔
679

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

687
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
583,242✔
688
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
583,242✔
689
  ((SExecTaskInfo*)tinfo)->paramSet = false;
583,242✔
690
}
583,242✔
691

692
int32_t qExecutorInit(void) {
4,070,141✔
693
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,070,141✔
694
  return TSDB_CODE_SUCCESS;
4,071,819✔
695
}
696

697
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
52,786,798✔
698
  int32_t        code = TSDB_CODE_SUCCESS;
52,786,798✔
699
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
52,786,798✔
700
  if (pTask->pWorkerCb) {
52,786,798✔
701
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
52,791,366✔
702
    if (code != TSDB_CODE_SUCCESS) {
52,822,185✔
703
      pTask->code = code;
×
704
      return pTask->code;
×
705
    }
706
  }
707

708
  code = tsem_wait(pSem);
52,841,085✔
709
  if (code != TSDB_CODE_SUCCESS) {
52,820,099✔
710
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
711
    pTask->code = code;
×
712
    return pTask->code;
×
713
  }
714

715
  if (pTask->pWorkerCb) {
52,820,099✔
716
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
52,823,418✔
717
    if (code != TSDB_CODE_SUCCESS) {
52,823,418✔
718
      pTask->code = code;
×
719
      return pTask->code;
×
720
    }
721
  }
722
  return TSDB_CODE_SUCCESS;
52,822,154✔
723
}
724

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

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

734
  readHandle->uid = 0;
353,538,054✔
735
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
353,562,556✔
736
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
353,253,273✔
737
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
8,776,724✔
738
    goto _error;
8,475,743✔
739
  }
740
    
741
  if (handle) {
344,602,231✔
742
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
344,491,146✔
743
    void*           pSinkManager = NULL;
344,604,928✔
744
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
343,976,262✔
745
    if (code != TSDB_CODE_SUCCESS) {
344,159,424✔
746
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
747
      goto _error;
×
748
    }
749

750
    void* pSinkParam = NULL;
344,159,424✔
751
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
344,397,995✔
752
    if (code != TSDB_CODE_SUCCESS) {
344,082,244✔
753
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
754
      taosMemoryFree(pSinkManager);
×
755
      goto _error;
×
756
    }
757

758
    SDataSinkNode* pSink = NULL;
344,082,244✔
759
    if (readHandle->localExec) {
344,328,996✔
760
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
2,816✔
761
      if (code != TSDB_CODE_SUCCESS) {
2,816✔
762
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
763
               (*pTask)->id.str);
764
        taosMemoryFree(pSinkManager);
×
765
        goto _error;
×
766
      }
767
    }
768

769
    // pSinkParam has been freed during create sinker.
770
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
344,370,109✔
771
                              (*pTask)->id.str, pSubplan->processOneBlock);
344,615,032✔
772
    if (code) {
344,201,900✔
773
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
558✔
774
    }
775
  }
776

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

780
_error:
148,393,948✔
781
  // if failed to add ref for all tables in this query, abort current query
782
  return code;
353,329,171✔
783
}
784

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

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

797
  if (pLocal) {
359,750,266✔
798
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
356,833,473✔
799
  }
800

801
  taosArrayClear(pResList);
359,427,769✔
802

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

810
  if (pTaskInfo->cost.start == 0) {
359,514,062✔
811
    pTaskInfo->cost.start = taosGetTimestampUs();
340,775,543✔
812
  }
813

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

820
  // error occurs, record the error code and return to client
821
  int32_t ret = setjmp(pTaskInfo->env);
359,628,673✔
822
  if (ret != TSDB_CODE_SUCCESS) {
367,405,528✔
823
    pTaskInfo->code = ret;
8,309,297✔
824
    (void)cleanUpUdfs();
8,309,297✔
825

826
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
8,309,297✔
827
    atomic_store_64(&pTaskInfo->owner, 0);
8,309,297✔
828

829
    return pTaskInfo->code;
8,309,297✔
830
  }
831

832
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
359,096,231✔
833

834
  int32_t      current = 0;
359,098,384✔
835
  SSDataBlock* pRes = NULL;
359,098,384✔
836
  int64_t      st = taosGetTimestampUs();
359,921,370✔
837

838
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
359,921,370✔
839
    pTaskInfo->paramSet = true;
583,242✔
840
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
583,242✔
841
  } else {
842
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
359,200,996✔
843
  }
844

845
  QUERY_CHECK_CODE(code, lino, _end);
351,664,929✔
846
  code = blockDataCheck(pRes);
351,664,929✔
847
  QUERY_CHECK_CODE(code, lino, _end);
351,746,780✔
848

849
  if (pRes == NULL) {
351,746,780✔
850
    st = taosGetTimestampUs();
79,528,987✔
851
  }
852

853
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
351,740,028✔
854
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
351,679,070✔
855
    rowsThreshold = 4096;
350,122,148✔
856
  }
857

858
  int32_t blockIndex = 0;
351,651,392✔
859
  while (pRes != NULL) {
872,951,622✔
860
    SSDataBlock* p = NULL;
537,663,711✔
861
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
537,618,216✔
862
      SSDataBlock* p1 = NULL;
451,259,394✔
863
      code = createOneDataBlock(pRes, true, &p1);
451,262,116✔
864
      QUERY_CHECK_CODE(code, lino, _end);
451,218,394✔
865

866
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
451,218,394✔
867
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
451,340,272✔
868
      p = p1;
451,340,272✔
869
    } else {
870
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
86,452,568✔
871
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
86,452,994✔
872

873
      p = *(SSDataBlock**)tmp;
86,452,994✔
874
      code = copyDataBlock(p, pRes);
86,452,461✔
875
      QUERY_CHECK_CODE(code, lino, _end);
86,452,562✔
876
    }
877

878
    blockIndex += 1;
537,789,880✔
879

880
    current += p->info.rows;
537,789,880✔
881
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
537,743,505✔
882
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
883
    void* tmp = taosArrayPush(pResList, &p);
537,781,395✔
884
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
537,781,395✔
885

886
    if (current >= rowsThreshold || processOneBlock) {
537,781,395✔
887
      break;
888
    }
889

890
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
521,320,614✔
891
    QUERY_CHECK_CODE(code, lino, _end);
521,305,996✔
892
    code = blockDataCheck(pRes);
521,305,996✔
893
    QUERY_CHECK_CODE(code, lino, _end);
521,332,759✔
894
  }
895

896
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
351,748,692✔
897
    pTaskInfo->pSubplan->rowsThreshold -= current;
1,574,500✔
898
  }
899

900
  *hasMore = (pRes != NULL);
351,764,508✔
901
  uint64_t el = (taosGetTimestampUs() - st);
351,709,049✔
902

903
  pTaskInfo->cost.elapsedTime += el;
351,709,049✔
904
  if (NULL == pRes) {
351,692,852✔
905
    *useconds = pTaskInfo->cost.elapsedTime;
335,232,071✔
906
  }
907

908
_end:
351,655,874✔
909
  (void)cleanUpUdfs();
351,597,297✔
910

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

915
  atomic_store_64(&pTaskInfo->owner, 0);
351,832,071✔
916
  if (code) {
351,832,077✔
917
    pTaskInfo->code = code;
×
918
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
919
  }
920

921
  return pTaskInfo->code;
351,832,077✔
922
}
923

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

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

938
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
121,047,928✔
939
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
121,047,928✔
940
  int64_t        threadId = taosGetSelfPthreadId();
121,047,928✔
941
  int64_t        curOwner = 0;
121,048,274✔
942

943
  *pRes = NULL;
121,048,274✔
944

945
  // todo extract method
946
  taosRLockLatch(&pTaskInfo->lock);
121,016,408✔
947
  bool isKilled = isTaskKilled(pTaskInfo);
121,049,361✔
948
  if (isKilled) {
121,048,697✔
949
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
950

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

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

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

963
  pTaskInfo->owner = threadId;
121,048,274✔
964
  taosRUnLockLatch(&pTaskInfo->lock);
121,048,606✔
965

966
  if (pTaskInfo->cost.start == 0) {
121,049,270✔
967
    pTaskInfo->cost.start = taosGetTimestampUs();
217,194✔
968
  }
969

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

980
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
121,036,488✔
981

982
  int64_t st = taosGetTimestampUs();
121,047,019✔
983
  int32_t code = TSDB_CODE_SUCCESS;
121,047,019✔
984
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
121,047,019✔
985
    pTaskInfo->paramSet = true;
×
986
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
987
  } else {
988
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
121,046,946✔
989
  }
990
  if (code) {
121,001,965✔
991
    pTaskInfo->code = code;
×
992
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
993
  }
994

995
  code = blockDataCheck(*pRes);
121,001,965✔
996
  if (code) {
121,043,504✔
997
    pTaskInfo->code = code;
×
998
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
999
  }
1000

1001
  uint64_t el = (taosGetTimestampUs() - st);
121,009,530✔
1002

1003
  pTaskInfo->cost.elapsedTime += el;
121,009,530✔
1004
  if (NULL == *pRes) {
121,031,648✔
1005
    *useconds = pTaskInfo->cost.elapsedTime;
18,966,037✔
1006
  }
1007

1008
  (void)cleanUpUdfs();
121,032,384✔
1009

1010
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
121,049,602✔
1011
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
121,049,602✔
1012

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

1016
  atomic_store_64(&pTaskInfo->owner, 0);
121,047,018✔
1017
  return pTaskInfo->code;
121,049,342✔
1018
}
1019

1020
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
107,723,246✔
1021
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
107,723,246✔
1022
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
107,725,744✔
1023
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
107,726,894✔
1024

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

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

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

1042
  return 0;
×
1043
}
1044

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

1054
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
324,778✔
1055
  if (pTaskInfo->subJobCtx.hasSubJobs) {
324,778✔
1056
    taosWLockLatch(&pTaskInfo->subJobCtx.lock);
300,259✔
1057
    if (pTaskInfo->subJobCtx.param) {
300,259✔
1058
      ((SScalarFetchParam*)pTaskInfo->subJobCtx.param)->pSubJobCtx = NULL;
137,957✔
1059
    }
1060
    pTaskInfo->subJobCtx.code = pTaskInfo->code;
300,259✔
1061
    int32_t code = tsem_post(&pTaskInfo->subJobCtx.ready);
300,259✔
1062
    taosWUnLockLatch(&pTaskInfo->subJobCtx.lock);
300,259✔
1063
  }
1064
  
1065
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
324,778✔
1066

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

1089
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
324,778✔
1090
}
324,778✔
1091

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

1098
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
324,778✔
1099

1100
  setTaskKilled(pTaskInfo, rspCode);
324,778✔
1101
  qStopTaskOperators(pTaskInfo);
324,778✔
1102

1103
  return TSDB_CODE_SUCCESS;
324,778✔
1104
}
1105

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

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

1119
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1120

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

1127
        taosMsleep(200);
×
1128

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

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

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

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

1159
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
345,777,523✔
1160
  STaskCostInfo* pSummary = &pTaskInfo->cost;
345,777,523✔
1161
  int64_t        idleTime = pSummary->start - pSummary->created;
345,843,656✔
1162

1163
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
345,783,812✔
1164
  if (pSummary->pRecoder != NULL) {
345,751,583✔
1165
    qDebug(
258,444,025✔
1166
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1167
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1168
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1169
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1170
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1171
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1172
  } else {
1173
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
87,287,219✔
1174
           pSummary->elapsedTime / 1000.0);
1175
  }
1176
}
345,731,244✔
1177

1178

1179
void qDestroyTask(qTaskInfo_t qTaskHandle) {
359,424,775✔
1180
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
359,424,775✔
1181
  if (pTaskInfo == NULL) {
359,424,775✔
1182
    return;
13,645,635✔
1183
  }
1184

1185
  if (pTaskInfo->pRoot != NULL) {
345,779,140✔
1186
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
345,857,829✔
1187
  } else {
1188
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1189
  }
1190

1191
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
345,857,494✔
1192
  doDestroyTask(pTaskInfo);
345,763,203✔
1193
}
1194

1195
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
4,220,297✔
1196
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
4,220,297✔
1197
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
4,220,297✔
1198
}
1199

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

1204
  while (1) {
301,233✔
1205
    uint16_t type = pOperator->operatorType;
608,137✔
1206
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
608,137✔
1207
      *scanner = pOperator->info;
306,904✔
1208
      break;
306,904✔
1209
    } else {
1210
      pOperator = pOperator->pDownstream[0];
301,233✔
1211
    }
1212
  }
1213
}
306,904✔
1214

1215
void* qExtractReaderFromTmqScanner(void* scanner) {
306,904✔
1216
  SStreamScanInfo* pInfo = scanner;
306,904✔
1217
  return (void*)pInfo->tqReader;
306,904✔
1218
}
1219

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

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

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

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

1246
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
708,908✔
1247
  memset(pCond, 0, sizeof(SQueryTableDataCond));
708,908✔
1248
  pCond->order = TSDB_ORDER_ASC;
708,908✔
1249
  pCond->numOfCols = pMtInfo->schema->nCols;
709,754✔
1250
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
709,754✔
1251
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
709,472✔
1252
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
710,318✔
1253
    taosMemoryFreeClear(pCond->colList);
1,137✔
1254
    taosMemoryFreeClear(pCond->pSlotList);
×
1255
    return terrno;
×
1256
  }
1257

1258
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
708,335✔
1259
  pCond->suid = pMtInfo->suid;
708,586✔
1260
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
708,868✔
1261
  pCond->startVersion = -1;
710,278✔
1262
  pCond->endVersion = sContext->snapVersion;
710,318✔
1263

1264
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,483,018✔
1265
    SColumnInfo* pColInfo = &pCond->colList[i];
3,773,255✔
1266
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
3,773,819✔
1267
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
3,775,793✔
1268
    if (pMtInfo->pExtSchemas != NULL) {
3,776,084✔
1269
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
43,760✔
1270
    }
1271
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
3,776,424✔
1272
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
3,776,415✔
1273

1274
    pCond->pSlotList[i] = i;
3,775,256✔
1275
  }
1276

1277
  return TSDB_CODE_SUCCESS;
710,600✔
1278
}
1279

1280
void qStreamSetOpen(qTaskInfo_t tinfo) {
119,593,742✔
1281
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
119,593,742✔
1282
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
119,593,742✔
1283
  pOperator->status = OP_NOT_OPENED;
119,606,757✔
1284
}
119,604,551✔
1285

1286
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
19,294,651✔
1287
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
19,294,651✔
1288
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
19,294,651✔
1289
}
19,296,205✔
1290

1291
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
20,242,365✔
1292
  int32_t        code = TSDB_CODE_SUCCESS;
20,242,365✔
1293
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
20,242,365✔
1294
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
20,242,365✔
1295

1296
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
20,244,549✔
1297
  const char*    id = GET_TASKID(pTaskInfo);
20,243,652✔
1298

1299
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
20,242,113✔
1300
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
19,010,572✔
1301
    if (pOperator == NULL || code != 0) {
19,009,058✔
1302
      return code;
260✔
1303
    }
1304

1305
    SStreamScanInfo* pInfo = pOperator->info;
19,008,798✔
1306
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
19,008,320✔
1307
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
19,007,461✔
1308
    walReaderVerifyOffset(pWalReader, pOffset);
19,011,810✔
1309
  }
1310
  // if pOffset equal to current offset, means continue consume
1311
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
20,241,738✔
1312
    return 0;
18,531,850✔
1313
  }
1314

1315
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1,709,139✔
1316
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
984,989✔
1317
    if (pOperator == NULL || code != 0) {
984,700✔
1318
      return code;
302✔
1319
    }
1320

1321
    SStreamScanInfo* pInfo = pOperator->info;
984,398✔
1322
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
984,398✔
1323
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
983,167✔
1324
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
983,766✔
1325

1326
    if (pOffset->type == TMQ_OFFSET__LOG) {
983,754✔
1327
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
767,462✔
1328
      pScanBaseInfo->dataReader = NULL;
767,128✔
1329

1330
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
767,462✔
1331
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
767,128✔
1332
      walReaderVerifyOffset(pWalReader, pOffset);
766,570✔
1333
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
767,462✔
1334
      if (code < 0) {
767,214✔
1335
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
10,464✔
1336
        return code;
10,464✔
1337
      }
1338
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
217,540✔
1339
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1340
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1341
      int64_t uid = pOffset->uid;
217,829✔
1342
      int64_t ts = pOffset->ts;
217,540✔
1343
      int32_t index = 0;
216,949✔
1344

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

1355
      if (uid == 0) {
216,634✔
1356
        if (numOfTables != 0) {
211,703✔
1357
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
36,959✔
1358
          if (!tmp) {
36,959✔
1359
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1360
            taosRUnLockLatch(&pTaskInfo->lock);
×
1361
            return terrno;
×
1362
          }
1363
          if (tmp) uid = tmp->uid;
36,959✔
1364
          ts = INT64_MIN;
36,959✔
1365
          pScanInfo->currentTable = 0;
36,959✔
1366
        } else {
1367
          taosRUnLockLatch(&pTaskInfo->lock);
174,744✔
1368
          qError("no table in table list, %s", id);
174,731✔
1369
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
175,335✔
1370
        }
1371
      }
1372
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
41,890✔
1373

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

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

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

1392
      STableKeyInfo keyInfo = {.uid = uid};
42,796✔
1393
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
42,796✔
1394

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

1403
      if (pScanBaseInfo->dataReader == NULL) {
42,796✔
1404
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
73,698✔
1405
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1406
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
36,849✔
1407
        if (code != TSDB_CODE_SUCCESS) {
36,549✔
1408
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1409
          return code;
×
1410
        }
1411

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

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

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

1437
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1438
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
724,150✔
1439
      SStreamRawScanInfo* pInfo = pOperator->info;
711,073✔
1440
      SSnapContext*       sContext = pInfo->sContext;
711,073✔
1441
      SOperatorInfo*      p = NULL;
711,395✔
1442

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

1448
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
711,395✔
1449

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

1455
      SMetaTableInfo mtInfo = {0};
711,395✔
1456
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
711,395✔
1457
      if (code != 0) {
711,113✔
1458
        destroyMetaTableInfo(&mtInfo);
1459
        return code;
×
1460
      }
1461
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
711,113✔
1462
      pInfo->dataReader = NULL;
710,822✔
1463

1464
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
711,104✔
1465
      tableListClear(pTableListInfo);
710,218✔
1466

1467
      if (mtInfo.uid == 0) {
710,831✔
1468
        destroyMetaTableInfo(&mtInfo);
1469
        goto end;  // no data
795✔
1470
      }
1471

1472
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
710,036✔
1473
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
708,908✔
1474
      if (code != TSDB_CODE_SUCCESS) {
708,908✔
1475
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1476
        destroyMetaTableInfo(&mtInfo);
1477
        return code;
×
1478
      }
1479
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
708,908✔
1480
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
1,236✔
1481
      } else {
1482
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
706,817✔
1483
      }
1484

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

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

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

1514
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
710,036✔
1515
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
710,600✔
1516
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1517
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
709,754✔
1518
      pTaskInfo->streamInfo.schema = mtInfo.schema;
710,036✔
1519
      taosMemoryFreeClear(mtInfo.pExtSchemas);
710,318✔
1520

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

1540
end:
1,523,910✔
1541
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
1,524,266✔
1542
  return 0;
1,524,266✔
1543
}
1544

1545
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
242,885,235✔
1546
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
242,885,235✔
1547
  if (pMsg->info.ahandle == NULL) {
243,005,855✔
1548
    rpcFreeCont(pMsg->pCont);
×
1549
    qError("pMsg->info.ahandle is NULL");
×
1550
    return;
×
1551
  }
1552

1553
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
242,790,728✔
1554
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1555

1556
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
242,799,201✔
1557

1558
  if (pMsg->contLen > 0) {
242,933,432✔
1559
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
242,870,396✔
1560
    if (buf.pData == NULL) {
242,668,716✔
1561
      pMsg->code = terrno;
×
1562
    } else {
1563
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
242,668,716✔
1564
    }
1565
  }
1566

1567
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
242,917,351✔
1568
  rpcFreeCont(pMsg->pCont);
243,055,692✔
1569
  destroySendMsgInfo(pSendInfo);
242,995,323✔
1570
}
1571

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

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

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

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

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

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

1600
  taosArrayDestroy(plist);
×
1601

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

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

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

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

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

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

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

1644
  *pList = NULL;
1,647,238✔
1645
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,647,874✔
1646
  if (pArray == NULL) {
1,647,874✔
1647
    return terrno;
×
1648
  }
1649

1650
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,647,874✔
1651
  if (code == 0) {
1,646,700✔
1652
    *pList = pArray;
1,646,700✔
1653
  } else {
1654
    taosArrayDestroy(pArray);
×
1655
  }
1656
  return code;
1,647,336✔
1657
}
1658

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

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

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

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

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

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

1694
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
25,952,762✔
1695
  int32_t code = 0;
25,952,762✔
1696

1697
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
25,952,762✔
1698
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
25,949,378✔
1699

1700
  if (pOper->fpSet.resetStateFn) {
25,948,997✔
1701
    code = pOper->fpSet.resetStateFn(pOper);
25,950,953✔
1702
  }
1703
  pOper->status = OP_NOT_OPENED;
25,947,093✔
1704
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
44,575,144✔
1705
    code = clearStatesForOperator(pOper->pDownstream[i]);
18,621,378✔
1706
  }
1707
  return code;
25,955,179✔
1708
}
1709

1710
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
7,331,720✔
1711
  int32_t        code = 0;
7,331,720✔
1712
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
7,331,720✔
1713
  SOperatorInfo* pOper = pTaskInfo->pRoot;
7,331,720✔
1714
  pTaskInfo->code = TSDB_CODE_SUCCESS;
7,331,921✔
1715
  code = clearStatesForOperator(pOper);
7,331,715✔
1716
  return code;
7,331,525✔
1717
}
1718

1719
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
10,036,820✔
1720
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,036,820✔
1721
  int64_t        threadId = taosGetSelfPthreadId();
10,036,820✔
1722
  int64_t        curOwner = 0;
10,037,239✔
1723

1724
  *ppRes = NULL;
10,037,239✔
1725

1726
  // todo extract method
1727
  taosRLockLatch(&pTaskInfo->lock);
10,037,239✔
1728
  bool isKilled = isTaskKilled(pTaskInfo);
10,037,044✔
1729
  if (isKilled) {
10,036,837✔
1730
    // clearStreamBlock(pTaskInfo->pRoot);
1731
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1732

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

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

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

1745
  pTaskInfo->owner = threadId;
10,036,407✔
1746
  taosRUnLockLatch(&pTaskInfo->lock);
10,036,820✔
1747

1748
  if (pTaskInfo->cost.start == 0) {
10,037,239✔
1749
    pTaskInfo->cost.start = taosGetTimestampUs();
206,262✔
1750
  }
1751

1752
  // error occurs, record the error code and return to client
1753
  int32_t ret = setjmp(pTaskInfo->env);
10,037,239✔
1754
  if (ret != TSDB_CODE_SUCCESS) {
11,823,453✔
1755
    pTaskInfo->code = ret;
1,787,467✔
1756
    (void)cleanUpUdfs();
1,787,467✔
1757
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,787,467✔
1758
    atomic_store_64(&pTaskInfo->owner, 0);
1,787,467✔
1759
    return pTaskInfo->code;
1,787,467✔
1760
  }
1761

1762
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
10,035,986✔
1763

1764
  int64_t st = taosGetTimestampUs();
10,037,033✔
1765

1766
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
10,037,033✔
1767
  if (code) {
8,249,358✔
1768
    pTaskInfo->code = code;
×
1769
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1770
  } else {
1771
    *finished = *ppRes == NULL;
8,249,358✔
1772
    code = blockDataCheck(*ppRes);
8,249,358✔
1773
  }
1774
  if (code) {
8,249,772✔
1775
    pTaskInfo->code = code;
×
1776
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1777
  }
1778

1779
  uint64_t el = (taosGetTimestampUs() - st);
8,249,772✔
1780

1781
  pTaskInfo->cost.elapsedTime += el;
8,249,772✔
1782
  if (NULL == *ppRes) {
8,249,772✔
1783
    *useconds = pTaskInfo->cost.elapsedTime;
5,111,334✔
1784
  }
1785

1786
  (void)cleanUpUdfs();
8,249,565✔
1787

1788
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
8,249,772✔
1789
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
8,249,772✔
1790

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

1794
  atomic_store_64(&pTaskInfo->owner, 0);
8,249,772✔
1795
  return pTaskInfo->code;
8,249,772✔
1796
}
1797

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

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

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

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

1830
end:
219,439✔
1831
  return 0;
219,439✔
1832
}
1833

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

1846
      void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
25,721✔
1847
      if (p == NULL) {
25,721✔
1848
        return terrno;
×
1849
      }
1850
    }
1851
  }
1852
  return code;
31,382✔
1853
}
1854

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

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

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

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

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

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

1932
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,220,111✔
1933

1934
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,081,995✔
1935
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,081,995✔
1936
  qDestroyTask(tInfo);
2,081,995✔
1937
}
2,081,995✔
1938

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

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

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

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

2007
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
1,981,521✔
2008
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
1,981,521✔
2009
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
1,981,521✔
2010
  int32_t        code = 0;
1,981,521✔
2011
  SNode*         pNode = NULL;
1,981,521✔
2012
  if (!pForceOutputCols) return 0;
1,981,521✔
2013
  if (!*pRes) {
37,048✔
2014
    code = createDataBlock(pRes);
37,048✔
2015
  }
2016

2017
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
37,048✔
2018
    int32_t idx = 0;
37,048✔
2019
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
148,012✔
2020
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
110,964✔
2021
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
110,964✔
2022
      colInfo.info.precision = pCol->type.precision;
110,964✔
2023
      colInfo.info.scale = pCol->type.scale;
110,964✔
2024
      code = blockDataAppendColInfo(*pRes, &colInfo);
110,964✔
2025
      if (code != 0) break;
110,964✔
2026
    }
2027
  }
2028

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

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

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

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

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

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

2194
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
201,187✔
2195
  int32_t code = 0, lino = 0;
201,187✔
2196
  if (ppDst == NULL || pSrc == NULL) {
201,187✔
2197
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2198
  }
2199
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
201,187✔
2200
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
201,946✔
2201

2202
  (*ppDst)->suid = pSrc->suid;
201,946✔
2203
  (*ppDst)->sver = pSrc->sver;
201,946✔
2204
  (*ppDst)->tbType = pSrc->tbType;
201,946✔
2205
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
201,946✔
2206
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
201,946✔
2207

2208
  if (pSrc->stbname) {
201,946✔
2209
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
201,946✔
2210
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
201,946✔
2211
  }
2212

2213
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
201,946✔
2214
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
201,946✔
2215

2216
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
201,946✔
2217

2218
  if (pSrc->pFields && pSrc->pFields->size > 0) {
201,946✔
2219
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
201,946✔
2220
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
201,946✔
2221
  } else {
2222
    (*ppDst)->pFields = NULL;
×
2223
  }
2224
  
2225
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
201,946✔
2226
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
124,357✔
2227
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
124,357✔
2228
  } else {
2229
    (*ppDst)->pTagFields = NULL;
77,589✔
2230
  }
2231

2232
  if (pSrc->colCids && pSrc->colCids->size > 0) {
201,946✔
2233
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,655✔
2234
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,655✔
2235
  } else {
2236
    (*ppDst)->colCids = NULL;
200,291✔
2237
  }
2238

2239
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
201,946✔
2240
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,063✔
2241
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,063✔
2242
  } else {
2243
    (*ppDst)->tagCids = NULL;
200,681✔
2244
  }
2245

2246
_exit:
201,946✔
2247

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

2259
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
236✔
2260
  return doDropStreamTable(pMsgCb, pOutput, pReq);
236✔
2261
}
2262

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

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

2280
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
4,264✔
2281
  pList->idInfo.suid = suid;
4,264✔
2282
  pList->idInfo.tableType = TD_SUPER_TABLE;
4,264✔
2283
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, &((SExecTaskInfo*)pTaskInfo)->storageAPI);
4,264✔
2284
  if (code != TSDB_CODE_SUCCESS) {
4,264✔
2285
    goto end;
×
2286
  }                                              
2287
  // taosArrayClear(uidList);
2288
  // for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
2289
  //   void* tmp = taosArrayGet(uidListCopy, i);
2290
  //   if (tmp == NULL) {
2291
  //     continue;
2292
  //   }
2293
  //   int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
2294
  //   if (slot == NULL) {
2295
  //     if (taosArrayPush(uidList, tmp) == NULL) {
2296
  //       code = terrno;
2297
  //       goto end;
2298
  //     }
2299
  //   }
2300
  // }
2301
end:
4,264✔
2302
  // taosArrayDestroy(uidListCopy);
2303
  tableListDestroy(pList);
4,264✔
2304
  return code;
4,264✔
2305
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc