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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

70.85
/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 "ttypes.h"
37
#include "tudf.h"
38
#include "wal.h"
39

40
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
41
int32_t             fetchObjRefPool = -1;
42
SGlobalExecInfo     gExecInfo = {0};
43

44
void setTaskScalarExtraInfo(qTaskInfo_t tinfo) {
745,752,514✔
45
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
745,752,514✔
46
  gTaskScalarExtra.pSubJobCtx = pTaskInfo->pSubJobCtx;
745,752,514✔
47
  gTaskScalarExtra.fp = qFetchRemoteNode;
745,824,634✔
48
}
745,687,147✔
49

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

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

66
void doDestroyFetchObj(void* param) {
144,801,809✔
67
  if (param == NULL) {
144,801,809✔
68
    return;
×
69
  }
70

71
  if (*(bool*)param) {
144,801,809✔
72
    doDestroyExchangeOperatorInfo(param);
106,010,320✔
73
  } else {
74
    destroySubJobCtx((STaskSubJobCtx *)param);
38,795,097✔
75
  }
76
}
77

78
static void cleanupRefPool() {
582,591✔
79
  int32_t ref = atomic_val_compare_exchange_32(&fetchObjRefPool, fetchObjRefPool, 0);
582,591✔
80
  taosCloseRef(ref);
582,591✔
81
}
582,591✔
82

83
static void initRefPool() {
582,591✔
84
  fetchObjRefPool = taosOpenRef(1024, doDestroyFetchObj);
582,591✔
85
  (void)atexit(cleanupRefPool);
582,591✔
86
}
582,591✔
87

88
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
×
89
  int32_t code = TSDB_CODE_SUCCESS;
×
90
  int32_t lino = 0;
×
91
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
92
    if (pOperator->numOfDownstream == 0) {
×
93
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
94
      return TSDB_CODE_APP_ERROR;
×
95
    }
96

97
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
98
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
99
      return TSDB_CODE_APP_ERROR;
×
100
    }
101
    pOperator->status = OP_NOT_OPENED;
×
102
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
×
103
  } else {
104
    pOperator->status = OP_NOT_OPENED;
×
105
    return TSDB_CODE_SUCCESS;
×
106
  }
107

108
_end:
109
  if (code != TSDB_CODE_SUCCESS) {
110
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
111
  }
112
  return code;
113
}
114

115
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
116
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
117
    if (pOperator->numOfDownstream == 0) {
×
118
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
119
      return TSDB_CODE_APP_ERROR;
×
120
    }
121

122
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
123
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
124
      return TSDB_CODE_APP_ERROR;
×
125
    }
126

127
    pOperator->status = OP_NOT_OPENED;
×
128
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
129
  }
130
  return 0;
×
131
}
132

133
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
53,151,378✔
134
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
53,151,378✔
135
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
53,152,428✔
136
    STmqQueryScanInfo* pTmqScanInfo = pOperator->info;
24,234,695✔
137
    if (pTmqScanInfo->pTableScanOp != NULL) {
24,235,058✔
138
      STableScanInfo* pScanInfo = pTmqScanInfo->pTableScanOp->info;
24,234,337✔
139
      if (pScanInfo->base.dataReader != NULL) {
24,234,337✔
140
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
275,595✔
141
        if (code) {
275,595✔
142
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
143
          return code;
×
144
        }
145
      }
146
    }
147
  } else {
148
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
28,918,344✔
149
  }
150

151
  return 0;
28,237,865✔
152
}
153

154
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
28,236,815✔
155
  SExecTaskInfo* pTaskInfo = tinfo;
28,236,815✔
156
  pTaskInfo->id.queryId = queryId;
28,236,815✔
157
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
28,237,865✔
158

159
  // set the idstr for tsdbReader
160
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
28,237,771✔
161
}
162

163
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
164
  SExecTaskInfo* pTaskInfo = tinfo;
×
165
  return pTaskInfo->status == OP_EXEC_DONE;
×
166
}
167

168
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
169
  if (tinfo == NULL) {
×
170
    return TSDB_CODE_APP_ERROR;
×
171
  }
172

173
  if (pBlocks == NULL || numOfBlocks == 0) {
×
174
    return TSDB_CODE_SUCCESS;
×
175
  }
176

177
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
178

179
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
180
  if (code != TSDB_CODE_SUCCESS) {
×
181
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
182
  } else {
183
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
184
  }
185

186
  return code;
×
187
}
188

189
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, uint64_t id) {
471,252✔
190
  if (msg == NULL) {  // create raw scan
471,252✔
191
    SExecTaskInfo* pTaskInfo = NULL;
129,051✔
192

193
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
129,051✔
194
    if (NULL == pTaskInfo || code != 0) {
129,051✔
195
      return NULL;
×
196
    }
197

198
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
129,051✔
199
    if (NULL == pTaskInfo->pRoot || code != 0) {
129,051✔
200
      taosMemoryFree(pTaskInfo);
×
201
      return NULL;
×
202
    }
203

204
    pTaskInfo->storageAPI = pReaderHandle->api;
129,051✔
205
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
129,051✔
206
    return pTaskInfo;
129,051✔
207
  }
208

209
  SSubplan* pPlan = NULL;
342,201✔
210
  int32_t   code = qStringToSubplan(msg, &pPlan);
344,838✔
211
  if (code != TSDB_CODE_SUCCESS) {
344,838✔
212
    qError("failed to parse subplan from msg, msg:%s code:%s", (char*) msg, tstrerror(code));
×
213
    terrno = code;
×
214
    return NULL;
×
215
  }
216

217
  qTaskInfo_t pTaskInfo = NULL;
344,838✔
218
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE, NULL);
344,838✔
219
  if (code != TSDB_CODE_SUCCESS) {
344,838✔
220
    qDestroyTask(pTaskInfo);
×
221
    terrno = code;
×
222
    return NULL;
×
223
  }
224

225
  return pTaskInfo;
344,838✔
226
}
227

228
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
725,336✔
229
  if (streamInserterParam == NULL) {
725,336✔
230
    return TSDB_CODE_SUCCESS;
460,321✔
231
  }
232

233
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
265,015✔
234
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
235
    return TSDB_CODE_INVALID_PARA;
×
236
  }
237

238
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
265,015✔
239
    stError("insertParam: invalid db/table name");
×
240
    return TSDB_CODE_INVALID_PARA;
×
241
  }
242

243
  if (streamInserterParam->suid <= 0 &&
265,015✔
244
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
107,672✔
245
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
246
    return TSDB_CODE_INVALID_PARA;
×
247
  }
248

249
  return TSDB_CODE_SUCCESS;
265,015✔
250
}
251

252
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
725,336✔
253
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
254
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
255
  if (pSubplan == NULL || pTaskInfo == NULL) {
725,336✔
256
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
×
257
    nodesDestroyNode((SNode *)pSubplan);
×
258
    return TSDB_CODE_INVALID_PARA;
×
259
  }
260
  int32_t lino = 0;
725,336✔
261
  int32_t code = checkInsertParam(streamInserterParam);
725,336✔
262
  if (code != TSDB_CODE_SUCCESS) {
725,336✔
263
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
264
    nodesDestroyNode((SNode *)pSubplan);
×
265
    return code;
×
266
  }
267
  SInserterParam* pInserterParam = NULL;
725,336✔
268
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
725,336✔
269
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
725,336✔
270
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
725,336✔
271

272
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, NULL);
725,336✔
273
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
725,219✔
274
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
234✔
UNCOV
275
    goto _error;
×
276
  }
277

278
  if (streamInserterParam) {
724,985✔
279
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
265,015✔
280
    void*           pSinkManager = NULL;
265,015✔
281
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
264,604✔
282
    if (code != TSDB_CODE_SUCCESS) {
264,780✔
283
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
284
      goto _error;
×
285
    }
286

287
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
264,780✔
288
    if (NULL == pInserterParam) {
264,839✔
289
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
290
      code = terrno;
×
291
      goto _error;
×
292
    }
293
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
264,839✔
294
    TSDB_CHECK_CODE(code, lino, _error);
265,015✔
295
    
296
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
265,015✔
297
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
264,839✔
298

299
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
264,839✔
300
    if (code) {
265,015✔
301
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
302
    }
303
  }
304
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
725,336✔
305
         tstrerror(code));
306

307
_error:
78,336✔
308

309
  if (code != TSDB_CODE_SUCCESS) {
725,336✔
UNCOV
310
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
UNCOV
311
    if (pInserterParam != NULL) {
×
312
      taosMemoryFree(pInserterParam);
×
313
    }
314
  }
315
  return code;
725,336✔
316
}
317

318
bool qNeedReset(qTaskInfo_t pInfo) {
3,277,735✔
319
  if (pInfo == NULL) {
3,277,735✔
320
    return false;
×
321
  }
322
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,277,735✔
323
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,277,735✔
324
  if (pOperator == NULL || pOperator->pPhyNode == NULL) {
3,277,735✔
325
    return false;
×
326
  }
327
  int32_t node = nodeType(pOperator->pPhyNode);
3,277,735✔
328
  return (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == node || 
2,974,478✔
329
          QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == node ||
559,052✔
330
          QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == node ||
6,252,213✔
331
          QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == node);
332
}
333

334
static void setReadHandle(SReadHandle* pHandle, STableScanBase* pScanBaseInfo) {
2,718,683✔
335
  if (pHandle == NULL || pScanBaseInfo == NULL) {
2,718,683✔
336
    return;
×
337
  }
338

339
  pScanBaseInfo->readHandle.uid = pHandle->uid;
2,718,683✔
340
  pScanBaseInfo->readHandle.winRangeValid = pHandle->winRangeValid;
2,718,683✔
341
  pScanBaseInfo->readHandle.winRange = pHandle->winRange;
2,718,683✔
342
  pScanBaseInfo->readHandle.extWinRangeValid = pHandle->extWinRangeValid;
2,718,683✔
343
  pScanBaseInfo->readHandle.extWinRange = pHandle->extWinRange;
2,718,683✔
344
  pScanBaseInfo->readHandle.cacheSttStatis = pHandle->cacheSttStatis;
2,718,683✔
345
}
346

347
int32_t qResetTableScan(qTaskInfo_t pInfo, SReadHandle* handle) {
3,277,735✔
348
  if (pInfo == NULL) {
3,277,735✔
349
    return TSDB_CODE_INVALID_PARA;
×
350
  }
351
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
3,277,735✔
352
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
3,277,735✔
353

354
  void*           info = pOperator->info;
3,277,735✔
355
  STableScanBase* pScanBaseInfo = NULL;
3,277,735✔
356

357
  if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pOperator->pPhyNode)) {
3,277,735✔
358
    pScanBaseInfo = &((STableScanInfo*)info)->base;
303,257✔
359
    setReadHandle(handle, pScanBaseInfo);
303,257✔
360
  } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(pOperator->pPhyNode)) {
2,974,478✔
361
    pScanBaseInfo = &((STableMergeScanInfo*)info)->base;
2,415,426✔
362
    setReadHandle(handle, pScanBaseInfo);
2,415,426✔
363
  }
364

365
  qDebug("reset table scan, name:%s, id:%s, time range: [%" PRId64 ", %" PRId64 "]", pOperator->name, GET_TASKID(pTaskInfo), handle->winRange.skey,
3,277,313✔
366
  handle->winRange.ekey);
367
  return pOperator->fpSet.resetStateFn(pOperator);
3,277,735✔
368
}
369

370
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
725,106✔
371
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
372
  if (msg == NULL) {
725,106✔
373
    return TSDB_CODE_INVALID_PARA;
×
374
  }
375

376
  *pTaskInfo = NULL;
725,106✔
377

378
  SSubplan* pPlan = NULL;
725,336✔
379
  int32_t   code = qStringToSubplan(msg, &pPlan);
725,336✔
380
  if (code != TSDB_CODE_SUCCESS) {
725,336✔
381
    nodesDestroyNode((SNode *)pPlan);
×
382
    return code;
×
383
  }
384
  // todo: add stream inserter param
385
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
725,336✔
386
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
387
                               pInserterParams);
388
  if (code != TSDB_CODE_SUCCESS) {
725,336✔
UNCOV
389
    qDestroyTask(*pTaskInfo);
×
UNCOV
390
    return code;
×
391
  }
392

393
  return code;
725,336✔
394
}
395

396
typedef struct {
397
  tb_uid_t tableUid;
398
  tb_uid_t childUid;
399
  int8_t   check;
400
} STqPair;
401

402
static int32_t filterUnqualifiedTables(const STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
392,336✔
403
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
404
  int32_t code = TSDB_CODE_SUCCESS;
392,336✔
405
  int32_t lino = 0;
392,336✔
406
  int8_t  locked = 0;
392,336✔
407
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
392,336✔
408

409
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
392,336✔
410

411
  SArray* tUid = taosArrayInit(4, sizeof(STqPair));
392,336✔
412
  QUERY_CHECK_NULL(tUid, code, lino, _error, terrno);
392,336✔
413

414
  int32_t numOfUids = taosArrayGetSize(tableIdList);
392,336✔
415
  if (numOfUids == 0) {
392,336✔
416
    (*ppArrayRes) = qa;
×
417
    goto _error;
×
418
  }
419

420
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
392,336✔
421

422
  uint64_t suid = 0;
392,336✔
423
  uint64_t uid = 0;
392,336✔
424
  int32_t  type = 0;
392,336✔
425
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
392,336✔
426

427
  // let's discard the tables those are not created according to the queried super table.
428
  SMetaReader mr = {0};
392,336✔
429
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
392,336✔
430

431
  locked = 1;
392,336✔
432
  for (int32_t i = 0; i < numOfUids; ++i) {
856,366✔
433
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
464,030✔
434
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
464,030✔
435

436
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
464,030✔
437
    if (code != TSDB_CODE_SUCCESS) {
464,030✔
438
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
439
      continue;
×
440
    }
441

442
    tDecoderClear(&mr.coder);
464,030✔
443

444
    if (mr.me.type == TSDB_SUPER_TABLE) {
464,030✔
445
      continue;
×
446
    } else {
447
      if (type == TSDB_SUPER_TABLE) {
464,030✔
448
        // this new created child table does not belong to the scanned super table.
449
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
464,030✔
450
          continue;
×
451
        }
452
      } else {  // ordinary table
453
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
454
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
455
        if (mr.me.uid != uid) {
×
456
          continue;
×
457
        }
458
      }
459
    }
460

461
    STqPair item = {.tableUid = *id, .childUid = mr.me.uid, .check = 0};
464,030✔
462
    if (pScanInfo->pTagCond != NULL) {
464,030✔
463
      // tb_uid_t id = mr.me.uid;
464
      item.check = 1;
351,790✔
465
    }
466
    if (taosArrayPush(tUid, &item) == NULL) {
464,030✔
467
      QUERY_CHECK_NULL(NULL, code, lino, _end, terrno);
×
468
    }
469
  }
470

471
  pAPI->metaReaderFn.clearReader(&mr);
392,336✔
472
  locked = 0;
392,336✔
473

474
  for (int32_t j = 0; j < taosArrayGetSize(tUid); ++j) {
856,366✔
475
    bool     qualified = false;
464,030✔
476
    STqPair* t = (STqPair*)taosArrayGet(tUid, j);
464,030✔
477
    if (t == NULL) {
464,030✔
478
      continue;
×
479
    }
480

481
    if (t->check == 1) {
464,030✔
482
      code = isQualifiedTable(t->childUid, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
351,790✔
483
      if (code != TSDB_CODE_SUCCESS) {
351,790✔
484
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", t->childUid, idstr);
×
485
        continue;
×
486
      }
487

488
      if (!qualified) {
351,790✔
489
        qInfo("table uid:0x%" PRIx64 " is unqualified for tag condition, %s", t->childUid, idstr);
175,753✔
490
        continue;
175,753✔
491
      }
492
    }
493

494
    void* tmp = taosArrayPush(qa, &t->tableUid);
288,277✔
495
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
288,277✔
496
  }
497

498
  // handle multiple partition
499

500
_end:
392,336✔
501

502
  if (locked) {
392,336✔
503
    pAPI->metaReaderFn.clearReader(&mr);
×
504
  }
505
  (*ppArrayRes) = qa;
392,336✔
506
_error:
392,336✔
507

508
  taosArrayDestroy(tUid);
392,336✔
509
  if (code != TSDB_CODE_SUCCESS) {
392,336✔
510
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
511
  }
512
  return code;
392,336✔
513
}
514

515
int32_t qDeleteTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
1,456✔
516
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1,456✔
517
  const char*    id = GET_TASKID(pTaskInfo);
1,456✔
518
  int32_t        code = 0;
1,456✔
519

520
  // traverse to the stream scanner node to add this table id
521
  SOperatorInfo* pInfo = NULL;
1,456✔
522
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
1,456✔
523
  if (code != 0 || pInfo == NULL) {
1,456✔
524
    return code;
×
525
  }
526

527
  STmqQueryScanInfo* pScanInfo = pInfo->info;
1,456✔
528
  qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
1,456✔
529
  taosWLockLatch(&pTaskInfo->lock);
1,456✔
530
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
1,456✔
531
  taosWUnLockLatch(&pTaskInfo->lock);
1,456✔
532

533
  return code;
1,456✔
534
}
535

536
static int32_t filterTableForTmqQuery(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, const char* id, SStorageAPI* pAPI, SRWLatch* lock) {
392,336✔
537
  SArray* qa = NULL;
392,336✔
538
  int32_t code = filterUnqualifiedTables(pScanInfo, tableIdList, id, pAPI, &qa);
392,336✔
539
  if (code != TSDB_CODE_SUCCESS) {
392,336✔
540
    taosArrayDestroy(qa);
×
541
    return code;
×
542
  }
543
  int32_t numOfQualifiedTables = taosArrayGetSize(qa);
392,336✔
544
  qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
392,336✔
545
  pAPI->tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
392,336✔
546

547
  bool   assignUid = false;
392,336✔
548
  size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
392,336✔
549
  char*  keyBuf = NULL;
392,336✔
550
  if (bufLen > 0) {
392,336✔
551
    assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
552
    keyBuf = taosMemoryMalloc(bufLen);
×
553
    if (keyBuf == NULL) {
×
554
      taosArrayDestroy(qa);
×
555
      return terrno;
×
556
    }
557
  }
558

559
  STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
392,336✔
560
  taosWLockLatch(lock);
392,336✔
561

562
  for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
680,613✔
563
    uint64_t* uid = taosArrayGet(qa, i);
288,277✔
564
    if (!uid) {
288,277✔
565
      taosMemoryFree(keyBuf);
×
566
      taosArrayDestroy(qa);
×
567
      taosWUnLockLatch(lock);
×
568
      return terrno;
×
569
    }
570
    STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
288,277✔
571

572
    if (bufLen > 0) {
288,277✔
573
      if (assignUid) {
×
574
        keyInfo.groupId = keyInfo.uid;
×
575
      } else {
576
        code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
577
                                      &keyInfo.groupId, pAPI);
578
        if (code != TSDB_CODE_SUCCESS) {
×
579
          taosMemoryFree(keyBuf);
×
580
          taosArrayDestroy(qa);
×
581
          taosWUnLockLatch(lock);
×
582
          return code;
×
583
        }
584
      }
585
    }
586

587
    code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
288,277✔
588
    if (code != TSDB_CODE_SUCCESS) {
288,277✔
589
      taosMemoryFree(keyBuf);
×
590
      taosArrayDestroy(qa);
×
591
      taosWUnLockLatch(lock);
×
592
      return code;
×
593
    }
594
  }
595

596
  taosWUnLockLatch(lock);
392,336✔
597
  if (keyBuf != NULL) {
392,336✔
598
    taosMemoryFree(keyBuf);
×
599
  }
600

601
  taosArrayDestroy(qa);
392,336✔
602
  return 0;
392,336✔
603
}
604

605
static void qUpdateTableTagCache(STmqQueryScanInfo* pScanInfo, const SArray* tableIdList, col_id_t cid, SStorageAPI* api) {
392,989✔
606
  STqReader*   tqReader = pScanInfo->tqReader;
392,989✔
607
  for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
857,672✔
608
    int64_t* uid = (int64_t*)taosArrayGet(tableIdList, i);
464,683✔
609
    api->tqReaderFn.tqUpdateTableTagCache(pScanInfo->tqReader, pScanInfo->pPseudoExpr, pScanInfo->numOfPseudoExpr, *uid, cid);
464,683✔
610
  }
611
}
392,989✔
612

613
int32_t qAddTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
392,336✔
614
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
392,336✔
615
  const char*    id = GET_TASKID(pTaskInfo);
392,336✔
616
  int32_t        code = 0;
392,336✔
617

618
  qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
392,336✔
619

620
  // traverse to the stream scanner node to add this table id
621
  SOperatorInfo* pInfo = NULL;
392,336✔
622
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
392,336✔
623
  if (code != 0 || pInfo == NULL) {
392,336✔
624
    return code;
×
625
  }
626

627
  STmqQueryScanInfo* pScanInfo = pInfo->info;
392,336✔
628
  qUpdateTableTagCache(pScanInfo, tableIdList, 0, &pTaskInfo->storageAPI);
392,336✔
629

630
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
392,336✔
631
}
632

633
void qUpdateTableTagCacheForTmq(qTaskInfo_t tinfo, const SArray* tableIdList, SArray* cids) {
653✔
634
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
653✔
635
  const char*    id = GET_TASKID(pTaskInfo);
653✔
636
  int32_t        code = 0;
653✔
637

638
  // traverse to the stream scanner node to add this table id
639
  SOperatorInfo* pInfo = NULL;
653✔
640
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
653✔
641
  if (code != 0 || pInfo == NULL) {
653✔
642
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
643
    return;
×
644
  }
645

646
  STmqQueryScanInfo* pScanInfo = pInfo->info;
653✔
647
  for (int32_t i = 0; i < taosArrayGetSize(cids); ++i) {
1,306✔
648
    col_id_t* cid = (col_id_t*)taosArrayGet(cids, i);
653✔
649
    qUpdateTableTagCache(pScanInfo, tableIdList, *cid, &pTaskInfo->storageAPI);
653✔
650
  }
651
}
652

653
int32_t qUpdateTableListForTmqScanner(qTaskInfo_t tinfo, const SArray* tableIdList) {
×
654
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
655
  const char*    id = GET_TASKID(pTaskInfo);
×
656
  int32_t        code = 0;
×
657

658
  // traverse to the stream scanner node to add this table id
659
  SOperatorInfo* pInfo = NULL;
×
660
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
×
661
  if (code != 0 || pInfo == NULL) {
×
662
    return code;
×
663
  }
664

665
  STmqQueryScanInfo* pScanInfo = pInfo->info;
×
666

667
  qDebug("%s %d remove child tables from the stream scanner, %s", __func__, (int32_t)taosArrayGetSize(tableIdList), id);
×
668
  taosWLockLatch(&pTaskInfo->lock);
×
669
  pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
670
  taosWUnLockLatch(&pTaskInfo->lock);
×
671
  
672
  return filterTableForTmqQuery(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &pTaskInfo->lock);
×
673
}
674

675
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
603,764,430✔
676
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
677
                                    int32_t idx, bool* tbGet) {
678
  *tbGet = false;
603,764,430✔
679

680
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
603,793,310✔
681
    return TSDB_CODE_INVALID_PARA;
18,493✔
682
  }
683
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
603,774,817✔
684

685
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
603,774,817✔
686
    return TSDB_CODE_SUCCESS;
345,659,874✔
687
  }
688

689
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
258,104,788✔
690
  if (!pSchemaInfo) {
258,161,757✔
691
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
692
    return terrno;
×
693
  }
694

695
  *sversion = pSchemaInfo->sw->version;
258,161,757✔
696
  *tversion = pSchemaInfo->tversion;
258,162,292✔
697
  *rversion = pSchemaInfo->rversion;
258,211,394✔
698
  if (pSchemaInfo->dbname) {
258,025,288✔
699
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
258,075,150✔
700
  } else {
701
    dbName[0] = 0;
×
702
  }
703
  if (pSchemaInfo->tablename) {
258,186,142✔
704
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
258,169,187✔
705
  } else {
UNCOV
706
    tableName[0] = 0;
×
707
  }
708

709
  *tbGet = true;
258,157,098✔
710

711
  return TSDB_CODE_SUCCESS;
258,093,137✔
712
}
713

714
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
345,631,966✔
715

716
void qDestroyOperatorParam(SOperatorParam* pParam) {
126,480✔
717
  if (NULL == pParam) {
126,480✔
718
    return;
×
719
  }
720
  freeOperatorParam(pParam, OP_GET_PARAM);
126,480✔
721
}
722

723
/**
724
  @brief Update the operator param for the task.
725
  @note  Unlike setOperatorParam, this function will destroy the new param when
726
         operator type mismatch.
727
*/
728
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
36,688,175✔
729
  SExecTaskInfo* pTask = (SExecTaskInfo*)tinfo;
36,688,175✔
730
  SOperatorParam* pNewParam = (SOperatorParam*)pParam;
36,688,175✔
731
  if (pTask->pRoot && pTask->pRoot->operatorType != pNewParam->opType) {
36,688,175✔
732
    qError("%s, %s operator type mismatch, task operator type:%d, "
122,016✔
733
           "new param operator type:%d", GET_TASKID(pTask), __func__,
734
           pTask->pRoot->operatorType,
735
           pNewParam->opType);
736
    qDestroyOperatorParam((SOperatorParam*)pParam);
122,016✔
737
    return;
122,016✔
738
  }
739
  TSWAP(pParam, pTask->pOpParam);
36,573,736✔
740
  ((SExecTaskInfo*)tinfo)->paramSet = false;
36,574,912✔
741
}
742

743
int32_t qExecutorInit(void) {
4,582,066✔
744
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
4,582,066✔
745
  return TSDB_CODE_SUCCESS;
4,583,065✔
746
}
747

748
int32_t qSemWait(qTaskInfo_t task, tsem_t* pSem) {
36,327,556✔
749
  int32_t        code = TSDB_CODE_SUCCESS;
36,327,556✔
750
  SExecTaskInfo* pTask = (SExecTaskInfo*)task;
36,327,556✔
751
  if (pTask->pWorkerCb) {
36,327,556✔
752
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
36,329,200✔
753
    if (code != TSDB_CODE_SUCCESS) {
36,329,748✔
754
      pTask->code = code;
×
755
      return pTask->code;
×
756
    }
757
  }
758

759
  code = tsem_wait(pSem);
36,329,748✔
760
  if (code != TSDB_CODE_SUCCESS) {
36,329,212✔
761
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
762
    pTask->code = code;
×
763
    return pTask->code;
×
764
  }
765

766
  if (pTask->pWorkerCb) {
36,329,212✔
767
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
36,329,748✔
768
    if (code != TSDB_CODE_SUCCESS) {
36,330,284✔
769
      pTask->code = code;
×
770
      return pTask->code;
×
771
    }
772
  }
773
  return TSDB_CODE_SUCCESS;
36,329,748✔
774
}
775

776
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
348,183,320✔
777
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
778
                        EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
779
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
348,183,320✔
780
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
348,183,320✔
781

782
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d, subEndPoinsNum:%d", 
348,195,841✔
783
    taskId, pSubplan->id.queryId, vgId, (int32_t)taosArrayGetSize(subEndPoints ? *subEndPoints : NULL));
784

785
  readHandle->uid = 0;
348,245,117✔
786
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model, subEndPoints);
348,258,529✔
787
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
348,045,678✔
788
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
578,903✔
789
    goto _error;
469,514✔
790
  }
791
    
792
  if (handle) {
347,524,386✔
793
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
347,265,903✔
794
    void*           pSinkManager = NULL;
347,325,448✔
795
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
346,863,482✔
796
    if (code != TSDB_CODE_SUCCESS) {
347,035,025✔
797
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
798
      goto _error;
×
799
    }
800

801
    void* pSinkParam = NULL;
347,035,025✔
802
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
347,209,472✔
803
    if (code != TSDB_CODE_SUCCESS) {
346,842,308✔
804
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
805
      taosMemoryFree(pSinkManager);
×
806
      goto _error;
×
807
    }
808

809
    SDataSinkNode* pSink = NULL;
346,842,308✔
810
    if (readHandle->localExec) {
347,170,347✔
811
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
3,036✔
812
      if (code != TSDB_CODE_SUCCESS) {
3,036✔
813
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
×
814
               (*pTask)->id.str);
815
        taosMemoryFree(pSinkManager);
×
816
        goto _error;
×
817
      }
818
    }
819

820
    // pSinkParam has been freed during create sinker.
821
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
347,230,394✔
822
                              (*pTask)->id.str, pSubplan->processOneBlock);
347,235,240✔
823
    if (code) {
347,003,345✔
824
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
603✔
825
    }
826
  }
827

828
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s subEndPoints:%d", 
347,489,919✔
829
    taskId, pSubplan->id.queryId, tstrerror(code), (*pTask)->pSubJobCtx ? (int32_t)taosArrayGetSize((*pTask)->pSubJobCtx->subEndPoints) : 0);
830

831
_error:
124,224,538✔
832
  // if failed to add ref for all tables in this query, abort current query
833
  return code;
348,191,292✔
834
}
835

836
static void freeBlock(void* param) {
×
837
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
838
  blockDataDestroy(pBlock);
×
839
}
×
840

841
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
400,740,924✔
842
                     bool processOneBlock) {
843
  int32_t        code = TSDB_CODE_SUCCESS;
400,740,924✔
844
  int32_t        lino = 0;
400,740,924✔
845
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
400,740,924✔
846
  int64_t        threadId = taosGetSelfPthreadId();
400,740,924✔
847

848
  if (pLocal) {
400,710,172✔
849
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
396,879,687✔
850
  }
851

852
  taosArrayClear(pResList);
400,700,922✔
853

854
  int64_t curOwner = 0;
400,672,350✔
855
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
400,672,350✔
856
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
857
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
858
    return pTaskInfo->code;
×
859
  }
860

861
  if (pTaskInfo->cost.start == 0) {
400,645,519✔
862
    pTaskInfo->cost.start = taosGetTimestampUs();
341,605,617✔
863
  }
864

865
  if (isTaskKilled(pTaskInfo)) {
400,767,388✔
866
    atomic_store_64(&pTaskInfo->owner, 0);
×
867
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
868
    return pTaskInfo->code;
×
869
  }
870

871
  // error occurs, record the error code and return to client
872
  int32_t ret = setjmp(pTaskInfo->env);
400,639,729✔
873
  if (ret != TSDB_CODE_SUCCESS) {
402,733,477✔
874
    pTaskInfo->code = ret;
2,275,080✔
875
    (void)cleanUpUdfs();
2,275,080✔
876

877
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
2,275,080✔
878
    atomic_store_64(&pTaskInfo->owner, 0);
2,275,080✔
879

880
    return pTaskInfo->code;
2,275,080✔
881
  }
882

883
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
400,458,397✔
884

885
  int32_t      current = 0;
400,472,175✔
886
  SSDataBlock* pRes = NULL;
400,472,175✔
887
  int64_t      st = taosGetTimestampUs();
400,739,352✔
888

889
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
400,739,352✔
890
    pTaskInfo->paramSet = true;
36,566,400✔
891
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
36,565,405✔
892
  } else {
893
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
364,183,016✔
894
  }
895

896
  QUERY_CHECK_CODE(code, lino, _end);
398,473,611✔
897
  code = blockDataCheck(pRes);
398,473,611✔
898
  QUERY_CHECK_CODE(code, lino, _end);
398,495,214✔
899

900
  if (pRes == NULL) {
398,495,214✔
901
    st = taosGetTimestampUs();
82,605,651✔
902
  }
903

904
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
398,479,308✔
905
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
398,522,991✔
906
    rowsThreshold = 4096;
397,949,747✔
907
  }
908

909
  int32_t blockIndex = 0;
398,499,501✔
910
  while (pRes != NULL) {
1,112,164,003✔
911
    SSDataBlock* p = NULL;
763,263,104✔
912
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
763,178,110✔
913
      SSDataBlock* p1 = NULL;
621,113,741✔
914
      code = createOneDataBlock(pRes, true, &p1);
621,125,088✔
915
      QUERY_CHECK_CODE(code, lino, _end);
621,100,852✔
916

917
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
621,100,852✔
918
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
621,171,757✔
919
      p = p1;
621,171,757✔
920
    } else if (processOneBlock) {
142,197,930✔
921
      SSDataBlock** tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
17,857,303✔
922
      if (tmp) {
17,857,303✔
923
        blockDataDestroy(*tmp);
17,857,303✔
924
        *tmp = NULL;
17,857,303✔
925
      }
926
      SSDataBlock* p1 = NULL;
17,857,303✔
927
      code = createOneDataBlock(pRes, true, &p1);
17,857,303✔
928
      QUERY_CHECK_CODE(code, lino, _end);
17,857,303✔
929

930
      *tmp = p1;
17,857,303✔
931
      p = p1;
17,857,303✔
932
    } else {
933
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
124,340,627✔
934
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
124,340,304✔
935

936
      p = *(SSDataBlock**)tmp;
124,340,304✔
937
      code = copyDataBlock(p, pRes);
124,338,640✔
938
      QUERY_CHECK_CODE(code, lino, _end);
124,338,559✔
939
    }
940

941
    blockIndex += 1;
763,352,098✔
942

943
    current += p->info.rows;
763,352,098✔
944
    QUERY_CHECK_CONDITION((p->info.rows > 0), code, lino, _end,
763,352,470✔
945
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
946
    void* tmp = taosArrayPush(pResList, &p);
763,353,434✔
947
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
763,353,434✔
948

949
    if (current >= rowsThreshold || processOneBlock) {
763,353,434✔
950
      break;
951
    }
952

953
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
713,722,233✔
954
    QUERY_CHECK_CODE(code, lino, _end);
713,615,264✔
955
    code = blockDataCheck(pRes);
713,615,264✔
956
    QUERY_CHECK_CODE(code, lino, _end);
713,728,467✔
957
  }
958

959
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
398,532,100✔
960
    pTaskInfo->pSubplan->rowsThreshold -= current;
547,486✔
961
  }
962

963
  *hasMore = (pRes != NULL);
398,521,241✔
964
  uint64_t el = (taosGetTimestampUs() - st);
398,509,573✔
965

966
  pTaskInfo->cost.elapsedTime += el;
398,509,573✔
967
  if (NULL == pRes) {
398,539,934✔
968
    *useconds = pTaskInfo->cost.elapsedTime;
348,909,801✔
969
  }
970

971
_end:
398,558,530✔
972
  (void)cleanUpUdfs();
398,527,534✔
973

974
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
398,561,358✔
975
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
398,561,358✔
976
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
977

978
  atomic_store_64(&pTaskInfo->owner, 0);
398,561,358✔
979
  if (code) {
398,561,753✔
980
    pTaskInfo->code = code;
×
981
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
982
  }
983

984
  return pTaskInfo->code;
398,561,753✔
985
}
986

987
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
988
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
989
  SArray*        pList = pTaskInfo->pResultBlockList;
×
990
  size_t         num = taosArrayGetSize(pList);
×
991
  for (int32_t i = 0; i < num; ++i) {
×
992
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
993
    if (p) {
×
994
      blockDataDestroy(*p);
×
995
    }
996
  }
997

998
  taosArrayClear(pTaskInfo->pResultBlockList);
×
999
}
×
1000

1001
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
25,625,472✔
1002
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
25,625,472✔
1003
  int64_t        threadId = taosGetSelfPthreadId();
25,625,472✔
1004
  int64_t        curOwner = 0;
25,625,472✔
1005

1006
  *pRes = NULL;
25,625,472✔
1007

1008
  // todo extract method
1009
  taosRLockLatch(&pTaskInfo->lock);
25,625,472✔
1010
  bool isKilled = isTaskKilled(pTaskInfo);
25,625,472✔
1011
  if (isKilled) {
25,625,114✔
1012
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1013

1014
    taosRUnLockLatch(&pTaskInfo->lock);
×
1015
    return pTaskInfo->code;
×
1016
  }
1017

1018
  if (pTaskInfo->owner != 0) {
25,625,114✔
1019
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1020
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1021

1022
    taosRUnLockLatch(&pTaskInfo->lock);
×
1023
    return pTaskInfo->code;
×
1024
  }
1025

1026
  pTaskInfo->owner = threadId;
25,625,472✔
1027
  taosRUnLockLatch(&pTaskInfo->lock);
25,625,472✔
1028

1029
  if (pTaskInfo->cost.start == 0) {
25,625,472✔
1030
    pTaskInfo->cost.start = taosGetTimestampUs();
243,391✔
1031
  }
1032

1033
  // error occurs, record the error code and return to client
1034
  int32_t ret = setjmp(pTaskInfo->env);
25,625,472✔
1035
  if (ret != TSDB_CODE_SUCCESS) {
25,625,114✔
1036
    pTaskInfo->code = ret;
×
1037
    (void)cleanUpUdfs();
×
1038
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
1039
    atomic_store_64(&pTaskInfo->owner, 0);
×
1040
    return pTaskInfo->code;
×
1041
  }
1042

1043
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
25,625,114✔
1044

1045
  int64_t st = taosGetTimestampUs();
25,625,472✔
1046
  int32_t code = TSDB_CODE_SUCCESS;
25,625,472✔
1047
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
25,625,472✔
1048
    pTaskInfo->paramSet = true;
×
1049
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
1050
  } else {
1051
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
25,625,472✔
1052
  }
1053
  if (code) {
25,624,750✔
1054
    pTaskInfo->code = code;
×
1055
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1056
  }
1057

1058
  code = blockDataCheck(*pRes);
25,624,750✔
1059
  if (code) {
25,625,112✔
1060
    pTaskInfo->code = code;
×
1061
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1062
  }
1063

1064
  uint64_t el = (taosGetTimestampUs() - st);
25,624,087✔
1065

1066
  pTaskInfo->cost.elapsedTime += el;
25,624,087✔
1067
  if (NULL == *pRes) {
25,624,087✔
1068
    *useconds = pTaskInfo->cost.elapsedTime;
11,761,975✔
1069
  }
1070

1071
  (void)cleanUpUdfs();
25,624,087✔
1072

1073
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
25,625,472✔
1074
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
25,625,472✔
1075

1076
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
25,625,472✔
1077
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1078

1079
  atomic_store_64(&pTaskInfo->owner, 0);
25,625,472✔
1080
  return pTaskInfo->code;
25,625,472✔
1081
}
1082

1083
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
106,009,940✔
1084
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
106,009,940✔
1085
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
106,011,235✔
1086
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
106,011,718✔
1087

1088
  if (!tmp) {
106,009,940✔
1089
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1090
    return terrno;
×
1091
  }
1092
  return TSDB_CODE_SUCCESS;
106,009,940✔
1093
}
1094

1095
int32_t stopInfoComp(void const* lp, void const* rp) {
×
1096
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
1097
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
1098

1099
  if (key->refId < pInfo->refId) {
×
1100
    return -1;
×
1101
  } else if (key->refId > pInfo->refId) {
×
1102
    return 1;
×
1103
  }
1104

1105
  return 0;
×
1106
}
1107

1108
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
1109
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
1110
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
1111
  if (idx >= 0) {
×
1112
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
1113
  }
1114
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
1115
}
×
1116

1117
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
91,054✔
1118
  if (pTaskInfo->pSubJobCtx) {
91,054✔
1119
    pTaskInfo->pSubJobCtx->code = pTaskInfo->code;
21,372✔
1120
    int32_t code = tsem_post(&pTaskInfo->pSubJobCtx->ready);
21,372✔
1121
  }
1122
  
1123
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
91,054✔
1124

1125
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
91,054✔
1126
  for (int32_t i = 0; i < num; ++i) {
95,432✔
1127
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
4,378✔
1128
    if (!pStop) {
4,378✔
1129
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1130
      continue;
×
1131
    }
1132
    SExchangeInfo* pExchangeInfo = taosAcquireRef(fetchObjRefPool, pStop->refId);
4,378✔
1133
    if (pExchangeInfo) {
4,378✔
1134
      int32_t code = tsem_post(&pExchangeInfo->ready);
4,378✔
1135
      if (code != TSDB_CODE_SUCCESS) {
4,378✔
1136
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1137
      } else {
1138
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
4,378✔
1139
      }
1140
      code = taosReleaseRef(fetchObjRefPool, pStop->refId);
4,378✔
1141
      if (code != TSDB_CODE_SUCCESS) {
4,378✔
1142
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1143
      }
1144
    }
1145
  }
1146

1147
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
91,054✔
1148
}
91,054✔
1149

1150
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
91,054✔
1151
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
91,054✔
1152
  if (pTaskInfo == NULL) {
91,054✔
1153
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1154
  }
1155

1156
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
91,054✔
1157

1158
  setTaskKilled(pTaskInfo, rspCode);
91,054✔
1159
  qStopTaskOperators(pTaskInfo);
91,054✔
1160

1161
  return TSDB_CODE_SUCCESS;
91,054✔
1162
}
1163

1164
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
1165
  int64_t        st = taosGetTimestampMs();
×
1166
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1167
  if (pTaskInfo == NULL) {
×
1168
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1169
  }
1170

1171
  if (waitDuration > 0) {
×
1172
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1173
  } else {
1174
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1175
  }
1176

1177
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1178

1179
  if (waitDuration > 0) {
×
1180
    while (1) {
1181
      taosWLockLatch(&pTaskInfo->lock);
×
1182
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1183
        taosWUnLockLatch(&pTaskInfo->lock);
×
1184

1185
        taosMsleep(200);
×
1186

1187
        int64_t d = taosGetTimestampMs() - st;
×
1188
        if (d >= waitDuration && waitDuration >= 0) {
×
1189
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1190
          return TSDB_CODE_SUCCESS;
×
1191
        }
1192
      } else {  // not running now
1193
        pTaskInfo->code = rspCode;
×
1194
        taosWUnLockLatch(&pTaskInfo->lock);
×
1195
        return TSDB_CODE_SUCCESS;
×
1196
      }
1197
    }
1198
  }
1199

1200
  int64_t et = taosGetTimestampMs() - st;
×
1201
  if (et < waitDuration) {
×
1202
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1203
    return TSDB_CODE_SUCCESS;
×
1204
  }
1205
  return TSDB_CODE_SUCCESS;
×
1206
}
1207

1208
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1209
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1210
  if (NULL == pTaskInfo) {
×
1211
    return false;
×
1212
  }
1213

1214
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1215
}
1216

1217
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
348,652,460✔
1218
  STaskCostInfo* pSummary = &pTaskInfo->cost;
348,652,460✔
1219
  int64_t        idleTime = pSummary->start - pSummary->created;
348,666,799✔
1220

1221
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
348,671,814✔
1222
  if (pSummary->pRecoder != NULL) {
348,648,718✔
1223
    qDebug(
257,521,549✔
1224
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1225
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1226
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1227
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1228
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1229
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1230
  } else {
1231
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
91,106,453✔
1232
           pSummary->elapsedTime / 1000.0);
1233
  }
1234
}
348,628,397✔
1235

1236

1237
void qDestroyTask(qTaskInfo_t qTaskHandle) {
355,829,356✔
1238
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
355,829,356✔
1239
  if (pTaskInfo == NULL) {
355,829,356✔
1240
    return;
7,185,097✔
1241
  }
1242

1243
  if (pTaskInfo->pRoot != NULL) {
348,644,259✔
1244
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
348,664,125✔
1245
  } else {
1246
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1247
  }
1248

1249
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
348,665,480✔
1250
  doDestroyTask(pTaskInfo);
348,652,011✔
1251
}
1252

1253
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,864,112✔
1254
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,864,112✔
1255
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,864,112✔
1256
}
1257

1258
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
344,838✔
1259
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
344,838✔
1260
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
344,838✔
1261

1262
  while (1) {
338,625✔
1263
    uint16_t type = pOperator->operatorType;
683,463✔
1264
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
683,463✔
1265
      *scanner = pOperator->info;
344,838✔
1266
      break;
344,838✔
1267
    } else {
1268
      pOperator = pOperator->pDownstream[0];
338,625✔
1269
    }
1270
  }
1271
}
344,838✔
1272

1273
void* qExtractReaderFromTmqScanner(void* scanner) {
344,838✔
1274
  STmqQueryScanInfo* pInfo = scanner;
344,838✔
1275
  return (void*)pInfo->tqReader;
344,838✔
1276
}
1277

1278
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
769,956✔
1279
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
769,956✔
1280
  return pTaskInfo->tmqInfo.schema;
769,956✔
1281
}
1282

1283
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
769,956✔
1284
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
769,956✔
1285
  return pTaskInfo->tmqInfo.tbName;
769,956✔
1286
}
1287

1288
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
850,340✔
1289
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
850,340✔
1290
  return &pTaskInfo->tmqInfo.btMetaRsp;
850,340✔
1291
}
1292

1293
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
24,902,942✔
1294
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
24,902,942✔
1295
  tOffsetCopy(pOffset, &pTaskInfo->tmqInfo.currentOffset);
24,902,942✔
1296
  return 0;
24,903,300✔
1297
}
1298

1299
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
809,227✔
1300
  memset(pCond, 0, sizeof(SQueryTableDataCond));
809,227✔
1301
  pCond->order = TSDB_ORDER_ASC;
809,227✔
1302
  pCond->numOfCols = pMtInfo->schema->nCols;
810,471✔
1303
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
810,782✔
1304
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
809,849✔
1305
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
810,782✔
1306
    taosMemoryFreeClear(pCond->colList);
933✔
1307
    taosMemoryFreeClear(pCond->pSlotList);
×
1308
    return terrno;
×
1309
  }
1310

1311
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
809,849✔
1312
  pCond->suid = pMtInfo->suid;
810,471✔
1313
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
810,471✔
1314
  pCond->startVersion = -1;
810,471✔
1315
  pCond->endVersion = sContext->snapVersion;
809,538✔
1316

1317
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
5,157,583✔
1318
    SColumnInfo* pColInfo = &pCond->colList[i];
4,346,478✔
1319
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
4,347,100✔
1320
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
4,347,088✔
1321
    if (pMtInfo->pExtSchemas != NULL) {
4,346,777✔
1322
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
47,760✔
1323
    }
1324
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
4,346,765✔
1325
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
4,348,045✔
1326

1327
    pCond->pSlotList[i] = i;
4,347,734✔
1328
  }
1329

1330
  return TSDB_CODE_SUCCESS;
810,782✔
1331
}
1332

1333
void qStreamSetOpen(qTaskInfo_t tinfo) {
24,034,862✔
1334
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
24,034,862✔
1335
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
24,034,862✔
1336
  pOperator->status = OP_NOT_OPENED;
24,034,899✔
1337
}
24,033,945✔
1338

1339
void qStreamSetParams(qTaskInfo_t tinfo, int8_t sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
24,082,678✔
1340
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
24,082,678✔
1341
  pTaskInfo->tmqInfo.sourceExcluded = sourceExcluded;
24,082,678✔
1342
  pTaskInfo->tmqInfo.minPollRows = minPollRows;
24,083,754✔
1343
  pTaskInfo->tmqInfo.timeout = timeout;
24,074,625✔
1344
  pTaskInfo->tmqInfo.enableReplay = enableReplay;
24,077,832✔
1345
}
24,080,881✔
1346

1347
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
25,100,345✔
1348
  int32_t        code = TSDB_CODE_SUCCESS;
25,100,345✔
1349
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
25,100,345✔
1350
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
25,100,345✔
1351

1352
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
25,101,721✔
1353
  const char*    id = GET_TASKID(pTaskInfo);
25,101,358✔
1354

1355
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
25,100,638✔
1356
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
23,725,098✔
1357
    if (pOperator == NULL || code != 0) {
23,724,053✔
1358
      return code;
344✔
1359
    }
1360

1361
    STmqQueryScanInfo* pInfo = pOperator->info;
23,725,075✔
1362
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
23,725,431✔
1363
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
23,724,033✔
1364
    walReaderVerifyOffset(pWalReader, pOffset);
23,726,511✔
1365
  }
1366
  // if pOffset equal to current offset, means continue consume
1367
  if (tOffsetEqual(pOffset, &pTaskInfo->tmqInfo.currentOffset)) {
25,099,211✔
1368
    return 0;
22,998,323✔
1369
  }
1370

1371
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
2,098,159✔
1372
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,268,538✔
1373
    if (pOperator == NULL || code != 0) {
1,268,213✔
1374
      return code;
×
1375
    }
1376

1377
    STmqQueryScanInfo* pInfo = pOperator->info;
1,269,191✔
1378
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,268,543✔
1379
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,268,535✔
1380
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,268,208✔
1381

1382
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,267,568✔
1383
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,035,056✔
1384
      pScanBaseInfo->dataReader = NULL;
1,035,087✔
1385

1386
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
1,035,087✔
1387
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
1,035,414✔
1388
      walReaderVerifyOffset(pWalReader, pOffset);
1,034,136✔
1389
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
1,035,749✔
1390
      if (code < 0) {
1,035,749✔
1391
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
13,191✔
1392
        return code;
13,191✔
1393
      }
1394
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
232,789✔
1395
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1396
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1397
      int64_t uid = pOffset->uid;
233,442✔
1398
      int64_t ts = pOffset->ts;
233,442✔
1399
      int32_t index = 0;
233,112✔
1400

1401
      // this value may be changed if new tables are created
1402
      taosRLockLatch(&pTaskInfo->lock);
233,112✔
1403
      int32_t numOfTables = 0;
233,760✔
1404
      code = tableListGetSize(pTableListInfo, &numOfTables);
233,760✔
1405
      if (code != TSDB_CODE_SUCCESS) {
233,146✔
1406
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1407
        taosRUnLockLatch(&pTaskInfo->lock);
×
1408
        return code;
×
1409
      }
1410

1411
      if (uid == 0) {
233,146✔
1412
        if (numOfTables != 0) {
226,799✔
1413
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
41,210✔
1414
          if (!tmp) {
41,210✔
1415
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1416
            taosRUnLockLatch(&pTaskInfo->lock);
×
1417
            return terrno;
×
1418
          }
1419
          if (tmp) uid = tmp->uid;
41,210✔
1420
          ts = INT64_MIN;
41,536✔
1421
          pScanInfo->currentTable = 0;
41,536✔
1422
        } else {
1423
          taosRUnLockLatch(&pTaskInfo->lock);
185,589✔
1424
          qError("no table in table list, %s", id);
185,919✔
1425
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
185,919✔
1426
        }
1427
      }
1428
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
47,231✔
1429

1430
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
47,841✔
1431
             pInfo->pTableScanOp->resultInfo.totalRows);
1432
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
47,841✔
1433

1434
      // start from current accessed position
1435
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1436
      // position, let's find it from the beginning.
1437
      index = tableListFind(pTableListInfo, uid, 0);
47,841✔
1438
      taosRUnLockLatch(&pTaskInfo->lock);
47,841✔
1439

1440
      if (index >= 0) {
47,841✔
1441
        pScanInfo->currentTable = index;
47,841✔
1442
      } else {
1443
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1444
               numOfTables, pScanInfo->currentTable, id);
1445
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1446
      }
1447

1448
      STableKeyInfo keyInfo = {.uid = uid};
47,841✔
1449
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
47,841✔
1450

1451
      // let's start from the next ts that returned to consumer.
1452
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
47,841✔
1453
        pScanBaseInfo->cond.twindows.skey = ts;
964✔
1454
      } else {
1455
        pScanBaseInfo->cond.twindows.skey = ts + 1;
46,877✔
1456
      }
1457
      pScanInfo->scanTimes = 0;
47,841✔
1458

1459
      if (pScanBaseInfo->dataReader == NULL) {
47,841✔
1460
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
82,168✔
1461
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1462
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
41,084✔
1463
        if (code != TSDB_CODE_SUCCESS) {
41,084✔
1464
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1465
          return code;
×
1466
        }
1467

1468
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
41,084✔
1469
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1470
      } else {
1471
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
6,757✔
1472
        if (code != TSDB_CODE_SUCCESS) {
6,757✔
1473
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1474
          return code;
×
1475
        }
1476

1477
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
6,757✔
1478
        if (code != TSDB_CODE_SUCCESS) {
6,757✔
1479
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1480
          return code;
×
1481
        }
1482
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
6,757✔
1483
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1484
      }
1485

1486
      // restore the key value
1487
      pScanBaseInfo->cond.twindows.skey = oldSkey;
47,841✔
1488
    } else {
1489
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1490
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1491
    }
1492

1493
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1494
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
829,621✔
1495
      STmqRawScanInfo* pInfo = pOperator->info;
812,022✔
1496
      SSnapContext*       sContext = pInfo->sContext;
812,022✔
1497
      SOperatorInfo*      p = NULL;
812,022✔
1498

1499
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
812,022✔
1500
      if (code != 0) {
812,022✔
1501
        return code;
×
1502
      }
1503

1504
      STableListInfo* pTableListInfo = ((STmqRawScanInfo*)(p->info))->pTableListInfo;
812,022✔
1505

1506
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
812,022✔
1507
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1508
        return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
1509
      }
1510

1511
      SMetaTableInfo mtInfo = {0};
812,022✔
1512
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
812,022✔
1513
      if (code != 0) {
811,711✔
1514
        destroyMetaTableInfo(&mtInfo);
1515
        return code;
×
1516
      }
1517
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
811,711✔
1518
      pInfo->dataReader = NULL;
811,077✔
1519

1520
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
810,455✔
1521
      tableListClear(pTableListInfo);
811,711✔
1522

1523
      if (mtInfo.uid == 0) {
811,711✔
1524
        destroyMetaTableInfo(&mtInfo);
1525
        goto end;  // no data
1,240✔
1526
      }
1527

1528
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
810,471✔
1529
      code = initQueryTableDataCondForTmq(&pTaskInfo->tmqInfo.tableCond, sContext, &mtInfo);
809,849✔
1530
      if (code != TSDB_CODE_SUCCESS) {
810,782✔
1531
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1532
        destroyMetaTableInfo(&mtInfo);
1533
        return code;
×
1534
      }
1535
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
810,782✔
1536
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts;
1,377✔
1537
      } else {
1538
        pTaskInfo->tmqInfo.tableCond.twindows.skey = pOffset->ts + 1;
808,771✔
1539
      }
1540

1541
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
810,782✔
1542
      if (code != TSDB_CODE_SUCCESS) {
810,782✔
1543
        destroyMetaTableInfo(&mtInfo);
1544
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1545
        return code;
×
1546
      }
1547

1548
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
810,782✔
1549
      if (!pList) {
810,782✔
1550
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1551
        destroyMetaTableInfo(&mtInfo);
1552
        return code;
×
1553
      }
1554
      int32_t size = 0;
810,782✔
1555
      code = tableListGetSize(pTableListInfo, &size);
810,782✔
1556
      if (code != TSDB_CODE_SUCCESS) {
810,782✔
1557
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1558
        destroyMetaTableInfo(&mtInfo);
1559
        return code;
×
1560
      }
1561

1562
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->tmqInfo.tableCond, pList, size,
1,621,564✔
1563
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
810,782✔
1564
      if (code != TSDB_CODE_SUCCESS) {
810,160✔
1565
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1566
        destroyMetaTableInfo(&mtInfo);
1567
        return code;
×
1568
      }
1569

1570
      cleanupQueryTableDataCond(&pTaskInfo->tmqInfo.tableCond);
810,160✔
1571
      tstrncpy(pTaskInfo->tmqInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
810,782✔
1572
      //      pTaskInfo->tmqInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1573
      tDeleteSchemaWrapper(pTaskInfo->tmqInfo.schema);
809,526✔
1574
      pTaskInfo->tmqInfo.schema = mtInfo.schema;
810,148✔
1575
      taosMemoryFreeClear(mtInfo.pExtSchemas);
810,148✔
1576

1577
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
810,148✔
1578
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
17,599✔
1579
      STmqRawScanInfo* pInfo = pOperator->info;
5,753✔
1580
      SSnapContext*       sContext = pInfo->sContext;
5,753✔
1581
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
5,753✔
1582
      if (code != 0) {
5,753✔
1583
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1584
        return code;
×
1585
      }
1586
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
5,753✔
1587
             id);
1588
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
11,846✔
1589
      STmqRawScanInfo* pInfo = pOperator->info;
11,846✔
1590
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
11,846✔
1591
      pInfo->dataReader = NULL;
11,846✔
1592
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
11,846✔
1593
    }
1594
  }
1595

1596
end:
1,899,912✔
1597
  tOffsetCopy(&pTaskInfo->tmqInfo.currentOffset, pOffset);
1,900,020✔
1598
  return 0;
1,900,020✔
1599
}
1600

1601
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
266,606,257✔
1602
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
266,606,257✔
1603
  if (pMsg->info.ahandle == NULL) {
266,650,366✔
1604
    rpcFreeCont(pMsg->pCont);
×
1605
    qError("rsp msg got while pMsg->info.ahandle is NULL, 0x%" PRIx64 ":0x%" PRIx64, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
×
1606
    return;
×
1607
  }
1608

1609
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
266,566,349✔
1610
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1611

1612
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
266,583,716✔
1613

1614
  if (pMsg->contLen > 0) {
266,621,346✔
1615
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
266,608,036✔
1616
    if (buf.pData == NULL) {
266,561,505✔
1617
      pMsg->code = terrno;
×
1618
    } else {
1619
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
266,561,505✔
1620
    }
1621
  }
1622

1623
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
266,612,887✔
1624
  rpcFreeCont(pMsg->pCont);
266,679,109✔
1625
  destroySendMsgInfo(pSendInfo);
266,626,020✔
1626
}
1627

1628
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1629
  int32_t        code = TSDB_CODE_SUCCESS;
×
1630
  int32_t        lino = 0;
×
1631
  SExecTaskInfo* pTaskInfo = tinfo;
×
1632
  SArray*        plist = NULL;
×
1633

1634
  code = getTableListInfo(pTaskInfo, &plist);
×
1635
  if (code || plist == NULL) {
×
1636
    return NULL;
×
1637
  }
1638

1639
  // only extract table in the first elements
1640
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1641

1642
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1643
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1644

1645
  int32_t numOfTables = 0;
×
1646
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1647
  QUERY_CHECK_CODE(code, lino, _end);
×
1648

1649
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1650
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1651
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1652
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1653
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1654
  }
1655

1656
  taosArrayDestroy(plist);
×
1657

1658
_end:
×
1659
  if (code != TSDB_CODE_SUCCESS) {
×
1660
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1661
    T_LONG_JMP(pTaskInfo->env, code);
×
1662
  }
1663
  return pUidList;
×
1664
}
1665

1666
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
3,490,966✔
1667
  int32_t        code = TSDB_CODE_SUCCESS;
3,490,966✔
1668
  int32_t        lino = 0;
3,490,966✔
1669
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,490,966✔
1670

1671
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
3,491,524✔
1672
    STmqQueryScanInfo* pScanInfo = pOperator->info;
×
1673
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1674

1675
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1676
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1677
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
3,490,408✔
1678
    STableScanInfo* pScanInfo = pOperator->info;
1,745,203✔
1679

1680
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
1,745,761✔
1681
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,746,321✔
1682
  } else {
1683
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
1,745,203✔
1684
      code = extractTableList(pList, pOperator->pDownstream[0]);
1,745,763✔
1685
    }
1686
  }
1687

1688
_end:
×
1689
  if (code != TSDB_CODE_SUCCESS) {
3,491,522✔
1690
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1691
  }
1692
  return code;
3,490,406✔
1693
}
1694

1695
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
1,745,205✔
1696
  if (pList == NULL) {
1,745,205✔
1697
    return TSDB_CODE_INVALID_PARA;
×
1698
  }
1699

1700
  *pList = NULL;
1,745,205✔
1701
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
1,746,321✔
1702
  if (pArray == NULL) {
1,745,201✔
1703
    return terrno;
×
1704
  }
1705

1706
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
1,745,201✔
1707
  if (code == 0) {
1,745,763✔
1708
    *pList = pArray;
1,745,763✔
1709
  } else {
1710
    taosArrayDestroy(pArray);
×
1711
  }
1712
  return code;
1,745,763✔
1713
}
1714

1715
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1716
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1717
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1718
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1719
  }
1720
  return 0;
×
1721
}
1722

1723
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1724
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1725
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1726
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1727
  }
1728
  return 0;
×
1729
}
1730

1731
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1732
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1733

1734
  int32_t code = pTaskInfo->code;
×
1735
  pTaskInfo->code = 0;
×
1736
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1737
}
×
1738

1739
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1740
  int32_t code = 0;
×
1741
  return code;
×
1742
}
1743

1744
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1745
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1746
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1747
  return code;
×
1748
}
1749

1750
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
27,508,990✔
1751
  int32_t code = 0;
27,508,990✔
1752

1753
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
27,508,990✔
1754
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
27,506,680✔
1755

1756
  if (pOper->fpSet.resetStateFn) {
27,505,707✔
1757
    code = pOper->fpSet.resetStateFn(pOper);
27,507,804✔
1758
  }
1759
  pOper->status = OP_NOT_OPENED;
27,503,996✔
1760
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
47,004,579✔
1761
    code = clearStatesForOperator(pOper->pDownstream[i]);
19,493,862✔
1762
  }
1763
  return code;
27,510,790✔
1764
}
1765

1766
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
8,014,031✔
1767
  int32_t        code = 0;
8,014,031✔
1768
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
8,014,031✔
1769
  SOperatorInfo* pOper = pTaskInfo->pRoot;
8,014,031✔
1770
  pTaskInfo->code = TSDB_CODE_SUCCESS;
8,014,031✔
1771
  code = clearStatesForOperator(pOper);
8,014,031✔
1772
  return code;
8,013,801✔
1773
}
1774

1775
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
11,041,873✔
1776
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
11,041,873✔
1777
  int64_t        threadId = taosGetSelfPthreadId();
11,041,873✔
1778
  int64_t        curOwner = 0;
11,041,873✔
1779

1780
  *ppRes = NULL;
11,041,873✔
1781

1782
  // todo extract method
1783
  taosRLockLatch(&pTaskInfo->lock);
11,041,125✔
1784
  bool isKilled = isTaskKilled(pTaskInfo);
11,041,873✔
1785
  if (isKilled) {
11,041,125✔
1786
    // clearStreamBlock(pTaskInfo->pRoot);
1787
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1788

1789
    taosRUnLockLatch(&pTaskInfo->lock);
×
1790
    return pTaskInfo->code;
×
1791
  }
1792

1793
  if (pTaskInfo->owner != 0) {
11,041,125✔
1794
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1795
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1796

1797
    taosRUnLockLatch(&pTaskInfo->lock);
×
1798
    return pTaskInfo->code;
×
1799
  }
1800

1801
  pTaskInfo->owner = threadId;
11,040,895✔
1802
  taosRUnLockLatch(&pTaskInfo->lock);
11,041,873✔
1803

1804
  if (pTaskInfo->cost.start == 0) {
11,041,873✔
1805
    pTaskInfo->cost.start = taosGetTimestampUs();
328,821✔
1806
  }
1807

1808
  // error occurs, record the error code and return to client
1809
  int32_t ret = setjmp(pTaskInfo->env);
11,041,638✔
1810
  if (ret != TSDB_CODE_SUCCESS) {
12,702,297✔
1811
    pTaskInfo->code = ret;
1,662,125✔
1812
    (void)cleanUpUdfs();
1,662,125✔
1813
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,662,125✔
1814
    atomic_store_64(&pTaskInfo->owner, 0);
1,662,125✔
1815
    return pTaskInfo->code;
1,662,125✔
1816
  }
1817

1818
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
11,040,172✔
1819

1820
  int64_t st = taosGetTimestampUs();
11,041,638✔
1821

1822
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
11,041,638✔
1823
  if (code) {
9,379,748✔
1824
    pTaskInfo->code = code;
×
1825
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1826
  } else {
1827
    *finished = *ppRes == NULL;
9,379,748✔
1828
    code = blockDataCheck(*ppRes);
9,379,515✔
1829
  }
1830
  if (code) {
9,379,748✔
1831
    pTaskInfo->code = code;
×
1832
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1833
  }
1834

1835
  uint64_t el = (taosGetTimestampUs() - st);
9,379,515✔
1836

1837
  pTaskInfo->cost.elapsedTime += el;
9,379,515✔
1838
  if (NULL == *ppRes) {
9,379,748✔
1839
    *useconds = pTaskInfo->cost.elapsedTime;
5,847,346✔
1840
  }
1841

1842
  (void)cleanUpUdfs();
9,379,748✔
1843

1844
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
9,379,748✔
1845
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
9,379,748✔
1846

1847
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
9,379,748✔
1848
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1849

1850
  atomic_store_64(&pTaskInfo->owner, 0);
9,379,748✔
1851
  return pTaskInfo->code;
9,379,748✔
1852
}
1853

1854
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1855
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1856
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1857
// }
1858

1859
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
279,088✔
1860
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1861
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1862
  int32_t code = 0;                                        
279,088✔
1863
  if (*pTableListInfo != NULL) {
279,088✔
1864
    qDebug("table list already exists, no need to create again");
×
1865
    goto end;
×
1866
  }
1867
  STableListInfo* pList = tableListCreate();
279,088✔
1868
  if (pList == NULL) {
279,088✔
1869
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1870
    code = terrno;
×
1871
    goto end;
×
1872
  }
1873

1874
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
279,088✔
1875
  SReadHandle    pHandle = {.vnode = pVnode};
279,088✔
1876
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
279,088✔
1877

1878
  code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
279,088✔
1879
  if (code != 0) {
279,088✔
1880
    tableListDestroy(pList);
×
1881
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1882
    goto end;
×
1883
  }
1884
  *pTableListInfo = pList;
279,088✔
1885

1886
end:
279,088✔
1887
  return 0;
279,088✔
1888
}
1889

1890
static int32_t doFilterTableByTagCond(void* pVnode, STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, SStorageAPI* pStorageAPI){
33,265✔
1891
  int32_t code = doFilterByTagCond(pListInfo->idInfo.suid, pUidList, pTagCond, pVnode, SFLT_NOT_INDEX, pStorageAPI, NULL);
33,265✔
1892
  if (code != 0) {
33,265✔
1893
    return code;
×
1894
  }
1895
  int32_t numOfTables = taosArrayGetSize(pUidList);
33,265✔
1896
  for (int i = 0; i < numOfTables; i++) {
62,947✔
1897
    void* tmp = taosArrayGet(pUidList, i);
29,682✔
1898
    if (tmp == NULL) {
29,682✔
1899
      return terrno;
×
1900
    }
1901
    STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
29,682✔
1902

1903
    void* p = taosArrayPush(((STableListInfo*)pListInfo)->pTableList, &info);
29,682✔
1904
    if (p == NULL) {
29,682✔
1905
      return terrno;
×
1906
    }
1907
  }
1908
  return code;
33,265✔
1909
}
1910

1911
int32_t qStreamFilterTableListForReader(void* pVnode, SArray* uidList,
33,265✔
1912
                                        SNodeList* pGroupTags, SNode* pTagCond, SNode* pTagIndexCond,
1913
                                        SStorageAPI* storageAPI, SHashObj* groupIdMap, uint64_t suid, SArray** tableList) {
1914
  int32_t code = TSDB_CODE_SUCCESS;
33,265✔
1915
  SArray* uidListCopy = NULL;
33,265✔
1916
  STableListInfo* pList = tableListCreate();
33,265✔
1917
  if (pList == NULL) {
33,265✔
1918
    code = terrno;
×
1919
    goto end;
×
1920
  }
1921
  uidListCopy = taosArrayDup(uidList, NULL);
33,265✔
1922
  if (uidListCopy == NULL) {
33,265✔
1923
    code = terrno;
×
1924
    goto end;
×
1925
  }
1926
  SScanPhysiNode pScanNode = {.suid = suid, .tableType = TD_SUPER_TABLE};
33,265✔
1927
  SReadHandle    pHandle = {.vnode = pVnode};
33,265✔
1928

1929
  pList->idInfo.suid = suid;
33,265✔
1930
  pList->idInfo.tableType = TD_SUPER_TABLE;
33,030✔
1931
  code = doFilterTableByTagCond(pVnode, pList, uidList, pTagCond, storageAPI);
33,265✔
1932
  if (code != TSDB_CODE_SUCCESS) {
33,265✔
1933
    goto end;
×
1934
  }                                              
1935
  code = buildGroupIdMapForAllTables(pList, &pHandle, &pScanNode, pGroupTags, false, NULL, storageAPI, groupIdMap);
33,265✔
1936
  if (code != TSDB_CODE_SUCCESS) {
33,265✔
1937
    goto end;
×
1938
  }
1939
  *tableList = pList->pTableList;
33,265✔
1940
  pList->pTableList = NULL;
33,265✔
1941

1942
  taosArrayClear(uidList);
33,265✔
1943
  for (int32_t i = 0; i < taosArrayGetSize(uidListCopy); i++){
66,530✔
1944
    void* tmp = taosArrayGet(uidListCopy, i);
33,265✔
1945
    if (tmp == NULL) {
33,030✔
1946
      continue;
×
1947
    }
1948
    int32_t* slot = taosHashGet(pList->map, tmp, LONG_BYTES);
33,030✔
1949
    if (slot == NULL) {
33,265✔
1950
      if (taosArrayPush(uidList, tmp) == NULL) {
3,583✔
1951
        code = terrno;
×
1952
        goto end;
×
1953
      }
1954
    }
1955
  }
1956
end:
33,265✔
1957
  taosArrayDestroy(uidListCopy);
33,265✔
1958
  tableListDestroy(pList);
33,265✔
1959
  return code;
33,265✔
1960
}
1961

1962
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
1963
//   int32_t index = -1;
1964
//   (void)taosThreadRwlockRdlock(lock);
1965
//   if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
1966
//     index = 0;
1967
//     goto end;
1968
//   }
1969
//   for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
1970
//     int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
1971

1972
//     STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
1973
//     if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
1974
//       index = i;
1975
//       goto end;
1976
//     }
1977
//   }
1978
// end:
1979
//   (void)taosThreadRwlockUnlock(lock);
1980
//   return index;
1981
// }
1982

1983
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
279,088✔
1984
SArray*  qStreamGetTableListArray(void* pTableListInfo) {
279,088✔
1985
  STableListInfo* pList = pTableListInfo;
279,088✔
1986
  return pList->pTableList;
279,088✔
1987
}
1988

1989
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
1,580,571✔
1990

1991
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,929,620✔
1992
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
2,929,620✔
1993
  qDestroyTask(tInfo);
2,929,620✔
1994
}
2,929,620✔
1995

1996
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
783,967✔
1997
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
783,967✔
1998
}
1999

2000
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
844,127✔
2001
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
2002
  int32_t      code = 0;
844,127✔
2003
  SNode*       pNode = NULL;
844,127✔
2004
  SNodeList*   pList = NULL;
844,127✔
2005
  SExprInfo*   pExprInfo = NULL;
844,127✔
2006
  int32_t      numOfExprs = 1;
844,127✔
2007
  int32_t*     offset = NULL;
844,127✔
2008
  STargetNode* pTargetNode = NULL;
844,127✔
2009
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
843,951✔
2010
  if (code == 0) {
844,127✔
2011
    code = nodesCloneNode(pExpr, &pNode);
844,127✔
2012
  }
2013

2014
  if (code == 0) {
844,127✔
2015
    pTargetNode->dataBlockId = 0;
844,127✔
2016
    pTargetNode->pExpr = pNode;
844,127✔
2017
    pTargetNode->slotId = 0;
844,127✔
2018
  }
2019
  if (code == 0) {
844,127✔
2020
    code = nodesMakeList(&pList);
844,127✔
2021
  }
2022
  if (code == 0) {
844,127✔
2023
    code = nodesListAppend(pList, (SNode*)pTargetNode);
844,127✔
2024
  }
2025
  if (code == 0) {
844,127✔
2026
    pNode = NULL;
844,127✔
2027
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
844,127✔
2028
  }
2029

2030
  if (code == 0) {
844,127✔
2031
    const char* pVal = NULL;
844,127✔
2032
    int32_t     len = 0;
844,127✔
2033
    SNode*      pSclNode = NULL;
844,127✔
2034
    switch (pExprInfo->pExpr->nodeType) {
844,127✔
2035
      case QUERY_NODE_FUNCTION:
844,127✔
2036
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
844,127✔
2037
        break;
844,127✔
2038
      case QUERY_NODE_OPERATOR:
×
2039
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
2040
        break;
×
2041
      default:
×
2042
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2043
        break;
×
2044
    }
2045
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
844,127✔
2046
    SSDataBlock block = {0};
844,127✔
2047
    block.info.rows = 1;
844,127✔
2048
    SSDataBlock* pBlock = &block;
844,127✔
2049
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
844,127✔
2050
    if (tmp == NULL) {
844,127✔
2051
      code = terrno;
×
2052
    }
2053
    if (code == 0) {
844,127✔
2054
      gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
844,127✔
2055
      gTaskScalarExtra.pStreamRange = NULL;
844,127✔
2056
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, &gTaskScalarExtra);
844,127✔
2057
    }
2058
    taosArrayDestroy(pBlockList);
844,127✔
2059
  }
2060
  nodesDestroyList(pList);
844,127✔
2061
  destroyExprInfo(pExprInfo, numOfExprs);
843,454✔
2062
  taosMemoryFreeClear(pExprInfo);
843,454✔
2063
  return code;
844,127✔
2064
}
2065

2066
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
2,308,063✔
2067
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
2,308,063✔
2068
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
2,308,063✔
2069
  int32_t        code = 0;
2,308,333✔
2070
  SNode*         pNode = NULL;
2,308,333✔
2071
  if (!pForceOutputCols) return 0;
2,308,333✔
2072
  if (!*pRes) {
60,043✔
2073
    code = createDataBlock(pRes);
60,043✔
2074
  }
2075

2076
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
60,043✔
2077
    int32_t idx = 0;
60,043✔
2078
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
239,969✔
2079
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
179,926✔
2080
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
179,926✔
2081
      colInfo.info.precision = pCol->type.precision;
179,926✔
2082
      colInfo.info.scale = pCol->type.scale;
179,926✔
2083
      code = blockDataAppendColInfo(*pRes, &colInfo);
179,926✔
2084
      if (code != 0) break;
179,926✔
2085
    }
2086
  }
2087

2088
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
60,043✔
2089
  if (code != TSDB_CODE_SUCCESS) {
60,043✔
2090
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
2091
    return code;
×
2092
  }
2093

2094
  // loop all exprs for force output, execute all exprs
2095
  int32_t idx = 0;
60,043✔
2096
  int32_t rowIdx = (*pRes)->info.rows;
60,043✔
2097
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
60,043✔
2098
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
60,043✔
2099
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
239,969✔
2100
    SScalarParam   dst = {0};
179,926✔
2101
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
179,926✔
2102
    code = nodesStringToNode(pCol->expr, &pNode);
179,926✔
2103
    if (code != 0) break;
179,926✔
2104
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
179,926✔
2105
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
179,926✔
2106
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
119,883✔
2107
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
119,883✔
2108
    } else {
2109
      dst.columnData = pInfo;
60,043✔
2110
      dst.numOfRows = rowIdx;
60,043✔
2111
      dst.colAlloced = false;
60,043✔
2112
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
60,043✔
2113
    }
2114
    ++idx;
179,926✔
2115
    // TODO sclFreeParam(&dst);
2116
    nodesDestroyNode(pNode);
179,926✔
2117
    if (code != 0) break;
179,926✔
2118
  }
2119
  if (code == TSDB_CODE_SUCCESS) {
60,043✔
2120
    (*pRes)->info.rows++;
60,043✔
2121
  }
2122
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
60,043✔
2123
  return code;
60,043✔
2124
}
2125

2126
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
267,120✔
2127
  int32_t      code = 0;
267,120✔
2128
  const char*  pVal = NULL;
267,120✔
2129
  SScalarParam dst = {0};
267,120✔
2130
  int32_t      len = 0;
267,120✔
2131
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
267,120✔
2132
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
267,120✔
2133
  // execute the expr
2134
  switch (pExpr->type) {
267,120✔
2135
    case QUERY_NODE_VALUE: {
×
2136
      SValueNode* pValue = (SValueNode*)pExpr;
×
2137
      int32_t     type = pValue->node.resType.type;
×
2138
      if (!IS_STR_DATA_TYPE(type)) {
×
2139
        qError("invalid sub tb expr with non-str type");
×
2140
        code = TSDB_CODE_INVALID_PARA;
×
2141
        break;
×
2142
      }
2143
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
2144
      if (pTmp == NULL) {
×
2145
        qError("invalid sub tb expr with null value");
×
2146
        code = TSDB_CODE_INVALID_PARA;
×
2147
        break;
×
2148
      }
2149
      pVal = varDataVal(pTmp);
×
2150
      len = varDataLen(pTmp);
×
2151
    } break;
×
2152
    case QUERY_NODE_FUNCTION: {
267,120✔
2153
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
267,120✔
2154
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
267,120✔
2155
        qError("invalid sub tb expr with non-str type func");
×
2156
        code = TSDB_CODE_INVALID_PARA;
×
2157
        break;
×
2158
      }
2159
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
267,120✔
2160
      if (!pCol) {
267,120✔
2161
        code = terrno;
×
2162
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
2163
        break;
×
2164
      }
2165

2166
      pCol->hasNull = true;
267,120✔
2167
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
267,120✔
2168
      pCol->info.colId = 0;
267,120✔
2169
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
267,120✔
2170
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
267,120✔
2171
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
267,120✔
2172
      code = colInfoDataEnsureCapacity(pCol, 1, true);
267,120✔
2173
      if (code != 0) {
267,120✔
2174
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
2175
        taosMemoryFree(pCol);
×
2176
        break;
×
2177
      }
2178
      dst.columnData = pCol;
267,120✔
2179
      dst.numOfRows = 1;
267,120✔
2180
      dst.colAlloced = true;
267,120✔
2181
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
267,120✔
2182
      if (colDataIsNull_var(dst.columnData, 0)) {
267,120✔
2183
        qInfo("invalid sub tb expr with null value");
2,464✔
2184
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
2,640✔
2185
      }
2186
      if (code == 0) {
267,542✔
2187
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
264,480✔
2188
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
264,480✔
2189
      }
2190
    } break;
267,120✔
2191
    default:
×
2192
      qError("wrong subtable expr with type: %d", pExpr->type);
×
2193
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
2194
      break;
×
2195
  }
2196
  if (code == 0) {
267,120✔
2197
    if (!pVal || len == 0) {
264,229✔
2198
      qError("tbname generated with no characters which is not allowed");
×
2199
      code = TSDB_CODE_INVALID_PARA;
×
2200
    }
2201
    if(len > TSDB_TABLE_NAME_LEN - 1) {
264,229✔
2202
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
458✔
2203
      len = TSDB_TABLE_NAME_LEN - 1;
458✔
2204
    }
2205

2206
    memcpy(tbname, pVal, len);
264,229✔
2207
    tbname[len] = '\0';  // ensure null terminated
264,229✔
2208
    if (NULL != strchr(tbname, '.')) {
264,058✔
2209
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
2210
      qError("tbname generated with invalid characters, '.' is not allowed");
×
2211
    }
2212
  }
2213
  // TODO free dst
2214
  sclFreeParam(&dst);
266,949✔
2215
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
266,869✔
2216
  return code;
266,869✔
2217
}
2218

2219
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
265,015✔
2220
  if (pParam) {
265,015✔
2221
    if (pParam->tbname) {
265,015✔
2222
      taosMemFree(pParam->tbname);
265,015✔
2223
      pParam->tbname = NULL;
265,015✔
2224
    }
2225
    if (pParam->stbname) {
265,015✔
2226
      taosMemFree(pParam->stbname);
265,015✔
2227
      pParam->stbname = NULL;
265,015✔
2228
    }
2229
    if (pParam->dbFName) {
265,015✔
2230
      taosMemFree(pParam->dbFName);
265,015✔
2231
      pParam->dbFName = NULL;
265,015✔
2232
    }
2233
    if (pParam->pFields) {
265,015✔
2234
      taosArrayDestroy(pParam->pFields);
265,015✔
2235
      pParam->pFields = NULL;
265,015✔
2236
    }
2237
    if (pParam->pTagFields) {
265,015✔
2238
      taosArrayDestroy(pParam->pTagFields);
157,343✔
2239
      pParam->pTagFields = NULL;
157,343✔
2240
    }
2241
    if (pParam->colCids) {
265,015✔
2242
      taosArrayDestroy(pParam->colCids);
1,845✔
2243
      pParam->colCids = NULL;
1,845✔
2244
    }
2245
    if (pParam->tagCids) {
265,015✔
2246
      taosArrayDestroy(pParam->tagCids);
1,180✔
2247
      pParam->tagCids = NULL;
1,180✔
2248
    }
2249
    taosMemFree(pParam);
265,015✔
2250
  }
2251
}
265,015✔
2252

2253
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
265,015✔
2254
  int32_t code = 0, lino = 0;
265,015✔
2255
  if (ppDst == NULL || pSrc == NULL) {
265,015✔
2256
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2257
  }
2258
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
265,015✔
2259
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
265,015✔
2260

2261
  (*ppDst)->suid = pSrc->suid;
265,015✔
2262
  (*ppDst)->sver = pSrc->sver;
265,015✔
2263
  (*ppDst)->tbType = pSrc->tbType;
264,839✔
2264
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
265,015✔
2265
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
265,015✔
2266

2267
  if (pSrc->stbname) {
264,839✔
2268
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
265,015✔
2269
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
264,663✔
2270
  }
2271

2272
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
265,015✔
2273
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
265,015✔
2274

2275
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
264,722✔
2276

2277
  if (pSrc->pFields && pSrc->pFields->size > 0) {
265,015✔
2278
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
264,898✔
2279
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
265,015✔
2280
  } else {
2281
    (*ppDst)->pFields = NULL;
117✔
2282
  }
2283
  
2284
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
265,015✔
2285
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
157,167✔
2286
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
157,343✔
2287
  } else {
2288
    (*ppDst)->pTagFields = NULL;
107,848✔
2289
  }
2290

2291
  if (pSrc->colCids && pSrc->colCids->size > 0) {
265,015✔
2292
    (*ppDst)->colCids = taosArrayDup(pSrc->colCids, NULL);
1,845✔
2293
    TSDB_CHECK_NULL((*ppDst)->colCids, code, lino, _exit, terrno);
1,845✔
2294
  } else {
2295
    (*ppDst)->colCids = NULL;
263,170✔
2296
  }
2297

2298
  if (pSrc->tagCids && pSrc->tagCids->size > 0) {
264,839✔
2299
    (*ppDst)->tagCids = taosArrayDup(pSrc->tagCids, NULL);
1,180✔
2300
    TSDB_CHECK_NULL((*ppDst)->tagCids, code, lino, _exit, terrno);
1,180✔
2301
  } else {
2302
    (*ppDst)->tagCids = NULL;
263,835✔
2303
  }
2304

2305
_exit:
265,015✔
2306

2307
  if (code != 0) {
265,015✔
2308
    if (*ppDst) {
×
2309
      destroyStreamInserterParam(*ppDst);
×
2310
      *ppDst = NULL;
×
2311
    }
2312
    
2313
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2314
  }
2315
  return code;
265,015✔
2316
}
2317

2318
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
264✔
2319
  return doDropStreamTable(pMsgCb, pOutput, pReq);
264✔
2320
}
2321

2322
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2323
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2324
}
2325

2326
int32_t qFilterTableList(void* pVnode, SArray* uidList, SNode* node, void* pTaskInfo, uint64_t suid) {
12,066✔
2327
  int32_t         code = TSDB_CODE_SUCCESS;
12,066✔
2328

2329
  SNode* pTagCond = node == NULL ? NULL : ((SSubplan*)node)->pTagCond;
12,066✔
2330
  code = doFilterByTagCond(suid, uidList, pTagCond, pVnode, SFLT_NOT_INDEX, &((SExecTaskInfo*)pTaskInfo)->storageAPI, NULL);
12,066✔
2331
  if (code != TSDB_CODE_SUCCESS) {
12,066✔
2332
    goto end;
×
2333
  }
2334
end:
12,066✔
2335
  return code;
12,066✔
2336
}
2337

2338
bool isTrueForSatisfied(STrueForInfo* pTrueForInfo, int64_t skey, int64_t ekey, int64_t count) {
2,147,483,647✔
2339
  if (pTrueForInfo == NULL) {
2,147,483,647✔
2340
    return true;
2,147,483,647✔
2341
  }
2342

2343
  bool durationSatisfied = (pTrueForInfo->duration <= 0) || (llabs(ekey - skey) >= pTrueForInfo->duration);
2,147,483,647✔
2344
  bool countSatisfied = (pTrueForInfo->count <= 0) || (count >= pTrueForInfo->count);
2,147,483,647✔
2345
  switch (pTrueForInfo->trueForType) {
2,147,483,647✔
2346
    case TRUE_FOR_DURATION_ONLY:
2,147,483,647✔
2347
      return durationSatisfied;
2,147,483,647✔
2348
    case TRUE_FOR_COUNT_ONLY:
54,293,128✔
2349
      return countSatisfied;
54,293,128✔
2350
    case TRUE_FOR_AND:
54,293,128✔
2351
      return durationSatisfied && countSatisfied;
54,293,128✔
2352
    case TRUE_FOR_OR:
54,293,128✔
2353
      return durationSatisfied || countSatisfied;
54,293,128✔
UNCOV
2354
    default:
×
UNCOV
2355
      return true;
×
2356
  }
2357
}
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