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

taosdata / TDengine / #4791

13 Oct 2025 06:50AM UTC coverage: 57.628% (-0.8%) from 58.476%
#4791

push

travis-ci

web-flow
Merge pull request #33213 from taosdata/fix/huoh/timemoe_model_directory

fix: fix tdgpt timemoe model directory

136628 of 303332 branches covered (45.04%)

Branch coverage included in aggregate %.

208121 of 294900 relevant lines covered (70.57%)

4250784.02 hits per line

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

54.19
/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 "tdatablock.h"
32
#include "tref.h"
33
#include "trpc.h"
34
#include "tudf.h"
35
#include "wal.h"
36

37
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
38
int32_t             exchangeObjRefPool = -1;
39
SGlobalExecInfo     gExecInfo = {0};
40

41
void gExecInfoInit(void* pDnode, getDnodeId_f getDnodeId, getMnodeEpset_f getMnode) {
1,791✔
42
  gExecInfo.dnode = pDnode;
1,791✔
43
  gExecInfo.getMnode = getMnode;
1,791✔
44
  gExecInfo.getDnodeId = getDnodeId;
1,791✔
45
  return;
1,791✔
46
}
47

48
int32_t getCurrentMnodeEpset(SEpSet* pEpSet) {
148✔
49
  if (gExecInfo.dnode == NULL || gExecInfo.getMnode == NULL) {
148!
50
    qError("gExecInfo is not initialized");
×
51
    return TSDB_CODE_APP_ERROR;
×
52
  }
53
  gExecInfo.getMnode(gExecInfo.dnode, pEpSet);
148✔
54
  return TSDB_CODE_SUCCESS;
148✔
55
}
56

57
static void cleanupRefPool() {
1,680✔
58
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
1,680✔
59
  taosCloseRef(ref);
1,680✔
60
}
1,680✔
61

62
static void initRefPool() {
1,680✔
63
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
1,680✔
64
  (void)atexit(cleanupRefPool);
1,680✔
65
}
1,680✔
66

67
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
×
68
  int32_t code = TSDB_CODE_SUCCESS;
×
69
  int32_t lino = 0;
×
70
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
71
    if (pOperator->numOfDownstream == 0) {
×
72
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
73
      return TSDB_CODE_APP_ERROR;
×
74
    }
75

76
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
77
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
78
      return TSDB_CODE_APP_ERROR;
×
79
    }
80
    pOperator->status = OP_NOT_OPENED;
×
81
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
×
82
  } else {
83
    pOperator->status = OP_NOT_OPENED;
×
84

85
    SStreamScanInfo* pInfo = pOperator->info;
×
86

87
    if (type == STREAM_INPUT__MERGED_SUBMIT) {
×
88
      for (int32_t i = 0; i < numOfBlocks; i++) {
×
89
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
×
90
        void*        tmp = taosArrayPush(pInfo->pBlockLists, pReq);
×
91
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
92
      }
93
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
×
94
    } else if (type == STREAM_INPUT__DATA_SUBMIT) {
×
95
      void* tmp = taosArrayPush(pInfo->pBlockLists, &input);
×
96
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
97
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
×
98
    } else if (type == STREAM_INPUT__DATA_BLOCK) {
×
99
      for (int32_t i = 0; i < numOfBlocks; ++i) {
×
100
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
×
101
        SPackedData  tmp = {.pDataBlock = pDataBlock};
×
102
        void*        tmpItem = taosArrayPush(pInfo->pBlockLists, &tmp);
×
103
        QUERY_CHECK_NULL(tmpItem, code, lino, _end, terrno);
×
104
      }
105
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
×
106
    } else if (type == STREAM_INPUT__CHECKPOINT) {
×
107
      SPackedData tmp = {.pDataBlock = input};
×
108
      void*       tmpItem = taosArrayPush(pInfo->pBlockLists, &tmp);
×
109
      QUERY_CHECK_NULL(tmpItem, code, lino, _end, terrno);
×
110
      pInfo->blockType = STREAM_INPUT__CHECKPOINT;
×
111
    } else if (type == STREAM_INPUT__REF_DATA_BLOCK) {
×
112
      for (int32_t i = 0; i < numOfBlocks; ++i) {
×
113
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
×
114
        void*        tmp = taosArrayPush(pInfo->pBlockLists, pReq);
×
115
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
116
      }
117
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
×
118
    }
119

120
    return TSDB_CODE_SUCCESS;
×
121
  }
122

123
_end:
×
124
  if (code != TSDB_CODE_SUCCESS) {
×
125
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
126
  }
127
  return code;
×
128
}
129

130
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
×
131
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
132
    if (pOperator->numOfDownstream == 0) {
×
133
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
×
134
      return TSDB_CODE_APP_ERROR;
×
135
    }
136

137
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
×
138
      qError("join not supported for stream block scan, %s" PRIx64, id);
×
139
      return TSDB_CODE_APP_ERROR;
×
140
    }
141

142
    pOperator->status = OP_NOT_OPENED;
×
143
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
×
144
  }
145
  return 0;
×
146
}
147

148
int32_t doSetTaskId(SOperatorInfo* pOperator, SStorageAPI* pAPI) {
67,792✔
149
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
67,792✔
150
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
67,792✔
151
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
26,924✔
152
    if (pStreamScanInfo->pTableScanOp != NULL) {
26,924!
153
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
26,924✔
154
      if (pScanInfo->base.dataReader != NULL) {
26,924✔
155
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
693✔
156
        if (code) {
693!
157
          qError("failed to set reader id for executor, code:%s", tstrerror(code));
×
158
          return code;
×
159
        }
160
      }
161
    }
162
  } else {
163
    if (pOperator->pDownstream) return doSetTaskId(pOperator->pDownstream[0], pAPI);
40,868✔
164
  }
165

166
  return 0;
39,613✔
167
}
168

169
int32_t qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
39,613✔
170
  SExecTaskInfo* pTaskInfo = tinfo;
39,613✔
171
  pTaskInfo->id.queryId = queryId;
39,613✔
172
  buildTaskId(taskId, queryId, pTaskInfo->id.str, 64);
39,613✔
173

174
  // set the idstr for tsdbReader
175
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
39,612✔
176
}
177

178
bool qTaskIsDone(qTaskInfo_t tinfo) {
×
179
  SExecTaskInfo* pTaskInfo = tinfo;
×
180
  return pTaskInfo->status == OP_EXEC_DONE;
×
181
}
182

183
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
×
184
  if (tinfo == NULL) {
×
185
    return TSDB_CODE_APP_ERROR;
×
186
  }
187

188
  if (pBlocks == NULL || numOfBlocks == 0) {
×
189
    return TSDB_CODE_SUCCESS;
×
190
  }
191

192
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
193

194
  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
×
195
  if (code != TSDB_CODE_SUCCESS) {
×
196
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
×
197
  } else {
198
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
×
199
  }
200

201
  return code;
×
202
}
203

204
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
1,261✔
205
                                     uint64_t id) {
206
  if (msg == NULL) {  // create raw scan
1,261✔
207
    SExecTaskInfo* pTaskInfo = NULL;
301✔
208

209
    int32_t code = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE, &pReaderHandle->api, &pTaskInfo);
301✔
210
    if (NULL == pTaskInfo || code != 0) {
301!
211
      return NULL;
1✔
212
    }
213

214
    code = createTmqRawScanOperatorInfo(pReaderHandle, pTaskInfo, &pTaskInfo->pRoot);
300✔
215
    if (NULL == pTaskInfo->pRoot || code != 0) {
301!
216
      taosMemoryFree(pTaskInfo);
×
217
      return NULL;
×
218
    }
219

220
    pTaskInfo->storageAPI = pReaderHandle->api;
301✔
221
    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
301!
222
    return pTaskInfo;
301✔
223
  }
224

225
  SSubplan* pPlan = NULL;
960✔
226
  int32_t   code = qStringToSubplan(msg, &pPlan);
960✔
227
  if (code != TSDB_CODE_SUCCESS) {
957!
228
    terrno = code;
×
229
    return NULL;
×
230
  }
231

232
  qTaskInfo_t pTaskInfo = NULL;
957✔
233
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, 0, NULL, OPTR_EXEC_MODEL_QUEUE);
957✔
234
  if (code != TSDB_CODE_SUCCESS) {
961!
235
    qDestroyTask(pTaskInfo);
×
236
    terrno = code;
×
237
    return NULL;
×
238
  }
239

240
  // extract the number of output columns
241
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
961✔
242
  *numOfCols = 0;
961✔
243

244
  SNode* pNode;
245
  FOREACH(pNode, pDescNode->pSlots) {
8,730!
246
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
7,769✔
247
    if (pSlotDesc->output) {
7,769✔
248
      ++(*numOfCols);
7,767✔
249
    }
250
  }
251

252
  return pTaskInfo;
961✔
253
}
254

255
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
12,694✔
256
  if (streamInserterParam == NULL) {
12,694✔
257
    return TSDB_CODE_SUCCESS;
12,052✔
258
  }
259

260
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
642!
261
    stError("insertParam: invalid suid:%" PRIx64 " for child table", streamInserterParam->suid);
×
262
    return TSDB_CODE_INVALID_PARA;
×
263
  }
264

265
  if (streamInserterParam->dbFName == NULL || strlen(streamInserterParam->dbFName) == 0) {
642!
266
    stError("insertParam: invalid db/table name");
×
267
    return TSDB_CODE_INVALID_PARA;
×
268
  }
269

270
  if (streamInserterParam->suid <= 0 &&
644✔
271
      (streamInserterParam->tbname == NULL || strlen(streamInserterParam->tbname) == 0)) {
201!
272
    stError("insertParam: invalid table name, suid:%" PRIx64 "", streamInserterParam->suid);
×
273
    return TSDB_CODE_INVALID_PARA;
×
274
  }
275

276
  return TSDB_CODE_SUCCESS;
644✔
277
}
278

279
static int32_t qCreateStreamExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
12,694✔
280
                                     qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
281
                                     EOPTR_EXEC_MODEL model, SStreamInserterParam* streamInserterParam) {
282
  if (pSubplan == NULL || pTaskInfo == NULL) {
12,694!
283
    qError("invalid parameter, pSubplan:%p, pTaskInfo:%p", pSubplan, pTaskInfo);
×
284
    nodesDestroyNode((SNode *)pSubplan);
×
285
    return TSDB_CODE_INVALID_PARA;
×
286
  }
287
  int32_t lino = 0;
12,694✔
288
  int32_t code = checkInsertParam(streamInserterParam);
12,694✔
289
  if (code != TSDB_CODE_SUCCESS) {
12,696!
290
    qError("invalid stream inserter param, code:%s", tstrerror(code));
×
291
    nodesDestroyNode((SNode *)pSubplan);
×
292
    return code;
×
293
  }
294
  SInserterParam* pInserterParam = NULL;
12,696✔
295
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
12,696✔
296
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
12,696✔
297
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
12,695✔
298

299
  code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
12,697✔
300
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
12,698!
301
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
9!
302
    goto _error;
9✔
303
  }
304

305
  if (streamInserterParam) {
12,689✔
306
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
644✔
307
    void*           pSinkManager = NULL;
644✔
308
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
644✔
309
    if (code != TSDB_CODE_SUCCESS) {
644!
310
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
311
      goto _error;
×
312
    }
313

314
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
644!
315
    if (NULL == pInserterParam) {
644!
316
      qError("failed to taosMemoryCalloc, code:%s, %s", tstrerror(terrno), (*pTask)->id.str);
×
317
      code = terrno;
×
318
      goto _error;
×
319
    }
320
    code = cloneStreamInserterParam(&pInserterParam->streamInserterParam, streamInserterParam);
644✔
321
    TSDB_CHECK_CODE(code, lino, _error);
644!
322
    
323
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
644✔
324
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
644✔
325

326
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
644✔
327
    if (code) {
644!
328
      qError("failed to createStreamDataInserter, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
329
    }
330
  }
331
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
12,689✔
332
         tstrerror(code));
333

334
_error:
529✔
335

336
  if (code != TSDB_CODE_SUCCESS) {
12,698✔
337
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
9!
338
    if (pInserterParam != NULL) {
9!
339
      taosMemoryFree(pInserterParam);
×
340
    }
341
  }
342
  return code;
12,698✔
343
}
344

345
int32_t qResetTableScan(qTaskInfo_t* pInfo, STimeWindow range) {
×
346
  SExecTaskInfo*  pTaskInfo = (SExecTaskInfo*)pInfo;
×
347
  SOperatorInfo*  pOperator = pTaskInfo->pRoot;
×
348
  STableScanInfo* pScanInfo = pOperator->info;
×
349
  STableScanBase* pScanBaseInfo = &pScanInfo->base;
×
350

351
  if (range.skey != 0 && range.ekey != 0) {
×
352
    pScanBaseInfo->cond.twindows = range;
×
353
  }
354
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
×
355
  qStreamSetOpen(pTaskInfo);
×
356
  return pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
×
357
}
358

359
int32_t qCreateStreamExecTaskInfo(qTaskInfo_t* pTaskInfo, void* msg, SReadHandle* readers,
12,698✔
360
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
361
  if (msg == NULL) {
12,698!
362
    return TSDB_CODE_INVALID_PARA;
×
363
  }
364

365
  *pTaskInfo = NULL;
12,698✔
366

367
  SSubplan* pPlan = NULL;
12,698✔
368
  int32_t   code = qStringToSubplan(msg, &pPlan);
12,698✔
369
  if (code != TSDB_CODE_SUCCESS) {
12,695!
370
    nodesDestroyNode((SNode *)pPlan);
×
371
    return code;
×
372
  }
373
  // todo: add stream inserter param
374
  code = qCreateStreamExecTask(readers, vgId, taskId, pPlan, pTaskInfo,
12,695✔
375
                               pInserterParams ? &pInserterParams->pSinkHandle : NULL, 0, NULL, OPTR_EXEC_MODEL_STREAM,
376
                               pInserterParams);
377
  if (code != TSDB_CODE_SUCCESS) {
12,698✔
378
    qDestroyTask(*pTaskInfo);
9✔
379
    return code;
9✔
380
  }
381

382
  return code;
12,689✔
383
}
384

385
static int32_t filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr,
230✔
386
                                       SStorageAPI* pAPI, SArray** ppArrayRes) {
387
  int32_t code = TSDB_CODE_SUCCESS;
230✔
388
  int32_t lino = 0;
230✔
389
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
230✔
390
  QUERY_CHECK_NULL(qa, code, lino, _error, terrno);
230!
391
  int32_t numOfUids = taosArrayGetSize(tableIdList);
230✔
392
  if (numOfUids == 0) {
230!
393
    (*ppArrayRes) = qa;
×
394
    goto _error;
×
395
  }
396

397
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
230✔
398

399
  uint64_t suid = 0;
230✔
400
  uint64_t uid = 0;
230✔
401
  int32_t  type = 0;
230✔
402
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
230✔
403

404
  // let's discard the tables those are not created according to the queried super table.
405
  SMetaReader mr = {0};
230✔
406
  pAPI->metaReaderFn.initReader(&mr, pScanInfo->readHandle.vnode, META_READER_LOCK, &pAPI->metaFn);
230✔
407
  for (int32_t i = 0; i < numOfUids; ++i) {
514✔
408
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
284✔
409
    QUERY_CHECK_NULL(id, code, lino, _end, terrno);
284!
410

411
    int32_t code = pAPI->metaReaderFn.getTableEntryByUid(&mr, *id);
284✔
412
    if (code != TSDB_CODE_SUCCESS) {
284!
413
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
×
414
      continue;
×
415
    }
416

417
    tDecoderClear(&mr.coder);
284✔
418

419
    if (mr.me.type == TSDB_SUPER_TABLE) {
284!
420
      continue;
×
421
    } else {
422
      if (type == TSDB_SUPER_TABLE) {
284!
423
        // this new created child table does not belong to the scanned super table.
424
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
284!
425
          continue;
×
426
        }
427
      } else {  // ordinary table
428
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
429
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
430
        if (mr.me.uid != uid) {
×
431
          continue;
×
432
        }
433
      }
434
    }
435

436
    if (pScanInfo->pTagCond != NULL) {
284✔
437
      bool          qualified = false;
115✔
438
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
115✔
439
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
115✔
440
      if (code != TSDB_CODE_SUCCESS) {
115!
441
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
442
        continue;
57✔
443
      }
444

445
      if (!qualified) {
115✔
446
        continue;
57✔
447
      }
448
    }
449

450
    // handle multiple partition
451
    void* tmp = taosArrayPush(qa, id);
227✔
452
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
227!
453
  }
454

455
_end:
230✔
456

457
  pAPI->metaReaderFn.clearReader(&mr);
230✔
458
  (*ppArrayRes) = qa;
230✔
459

460
_error:
230✔
461
  if (code != TSDB_CODE_SUCCESS) {
230!
462
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
463
  }
464
  return code;
230✔
465
}
466

467
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
233✔
468
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
233✔
469
  const char*    id = GET_TASKID(pTaskInfo);
233✔
470
  int32_t        code = 0;
233✔
471

472
  if (isAdd) {
233✔
473
    qDebug("try to add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
230!
474
  }
475

476
  // traverse to the stream scanner node to add this table id
477
  SOperatorInfo* pInfo = NULL;
233✔
478
  code = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pInfo);
233✔
479
  if (code != 0 || pInfo == NULL) {
233!
480
    return code;
×
481
  }
482

483
  SStreamScanInfo* pScanInfo = pInfo->info;
233✔
484
  if (pInfo->pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {  // clear meta cache for subscription if tag is changed
233!
485
    for (int32_t i = 0; i < taosArrayGetSize(tableIdList); ++i) {
520✔
486
      int64_t*        uid = (int64_t*)taosArrayGet(tableIdList, i);
287✔
487
      STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
287✔
488
      taosLRUCacheErase(pTableScanInfo->base.metaCache.pTableMetaEntryCache, uid, LONG_BYTES);
287✔
489
    }
490
  }
491

492
  if (isAdd) {  // add new table id
233✔
493
    SArray* qa = NULL;
230✔
494
    code = filterUnqualifiedTables(pScanInfo, tableIdList, id, &pTaskInfo->storageAPI, &qa);
230✔
495
    if (code != TSDB_CODE_SUCCESS) {
230!
496
      taosArrayDestroy(qa);
×
497
      return code;
×
498
    }
499
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
230✔
500
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
230!
501
    pTaskInfo->storageAPI.tqReaderFn.tqReaderAddTables(pScanInfo->tqReader, qa);
230✔
502

503
    bool   assignUid = false;
230✔
504
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
230!
505
    char*  keyBuf = NULL;
230✔
506
    if (bufLen > 0) {
230!
507
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
×
508
      keyBuf = taosMemoryMalloc(bufLen);
×
509
      if (keyBuf == NULL) {
×
510
        taosArrayDestroy(qa);
×
511
        return terrno;
×
512
      }
513
    }
514

515
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
230✔
516
    taosWLockLatch(&pTaskInfo->lock);
230✔
517

518
    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
457✔
519
      uint64_t* uid = taosArrayGet(qa, i);
227✔
520
      if (!uid) {
227!
521
        taosMemoryFree(keyBuf);
×
522
        taosArrayDestroy(qa);
×
523
        taosWUnLockLatch(&pTaskInfo->lock);
×
524
        return terrno;
×
525
      }
526
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
227✔
527

528
      if (bufLen > 0) {
227!
529
        if (assignUid) {
×
530
          keyInfo.groupId = keyInfo.uid;
×
531
        } else {
532
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.vnode, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
×
533
                                       &keyInfo.groupId, &pTaskInfo->storageAPI);
534
          if (code != TSDB_CODE_SUCCESS) {
×
535
            taosMemoryFree(keyBuf);
×
536
            taosArrayDestroy(qa);
×
537
            taosWUnLockLatch(&pTaskInfo->lock);
×
538
            return code;
×
539
          }
540
        }
541
      }
542

543
      code = tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
227✔
544
      if (code != TSDB_CODE_SUCCESS) {
227!
545
        taosMemoryFree(keyBuf);
×
546
        taosArrayDestroy(qa);
×
547
        taosWUnLockLatch(&pTaskInfo->lock);
×
548
        return code;
×
549
      }
550
    }
551

552
    taosWUnLockLatch(&pTaskInfo->lock);
230✔
553
    if (keyBuf != NULL) {
230!
554
      taosMemoryFree(keyBuf);
×
555
    }
556

557
    taosArrayDestroy(qa);
230✔
558
  } else {  // remove the table id in current list
559
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
3!
560
    taosWLockLatch(&pTaskInfo->lock);
3✔
561
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
3✔
562
    taosWUnLockLatch(&pTaskInfo->lock);
3✔
563
  }
564

565
  return code;
233✔
566
}
567

568
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, int32_t dbNameBuffLen, char* tableName,
878,984✔
569
                                    int32_t tbaleNameBuffLen, int32_t* sversion, int32_t* tversion, int32_t* rversion,
570
                                    int32_t idx, bool* tbGet) {
571
  *tbGet = false;
878,984✔
572

573
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
878,984!
574
    return TSDB_CODE_INVALID_PARA;
×
575
  }
576
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
879,052✔
577

578
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
879,052✔
579
    return TSDB_CODE_SUCCESS;
509,679✔
580
  }
581

582
  SSchemaInfo* pSchemaInfo = taosArrayGet(pTaskInfo->schemaInfos, idx);
369,371✔
583
  if (!pSchemaInfo) {
369,381!
584
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
585
    return terrno;
×
586
  }
587

588
  *sversion = pSchemaInfo->sw->version;
369,391✔
589
  *tversion = pSchemaInfo->tversion;
369,391✔
590
  *rversion = pSchemaInfo->rversion;
369,391✔
591
  if (pSchemaInfo->dbname) {
369,391!
592
    tstrncpy(dbName, pSchemaInfo->dbname, dbNameBuffLen);
369,391✔
593
  } else {
594
    dbName[0] = 0;
×
595
  }
596
  if (pSchemaInfo->tablename) {
369,391!
597
    tstrncpy(tableName, pSchemaInfo->tablename, tbaleNameBuffLen);
369,397✔
598
  } else {
599
    tableName[0] = 0;
×
600
  }
601

602
  *tbGet = true;
369,391✔
603

604
  return TSDB_CODE_SUCCESS;
369,391✔
605
}
606

607
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
509,678✔
608

609
void qDestroyOperatorParam(SOperatorParam* pParam) {
×
610
  if (NULL == pParam) {
×
611
    return;
×
612
  }
613
  freeOperatorParam(pParam, OP_GET_PARAM);
×
614
}
615

616
void qUpdateOperatorParam(qTaskInfo_t tinfo, void* pParam) {
49,505✔
617
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
49,505✔
618
  ((SExecTaskInfo*)tinfo)->paramSet = false;
49,505✔
619
}
49,505✔
620

621
int32_t qExecutorInit(void) {
10,884✔
622
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
10,884✔
623
  return TSDB_CODE_SUCCESS;
10,887✔
624
}
625

626
int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
515,630✔
627
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, int8_t compressResult, char* sql,
628
                        EOPTR_EXEC_MODEL model) {
629
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
515,630✔
630
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
515,630✔
631

632
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
515,724✔
633

634
  readHandle->uid = 0;
516,079✔
635
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
516,079✔
636
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
515,909!
637
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
638
    goto _error;
19✔
639
  }
640

641
  if (handle) {
516,070✔
642
    SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50, .compress = compressResult};
515,140✔
643
    void*           pSinkManager = NULL;
515,140✔
644
    code = dsDataSinkMgtInit(&cfg, &(*pTask)->storageAPI, &pSinkManager);
515,140✔
645
    if (code != TSDB_CODE_SUCCESS) {
515,159!
646
      qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
×
647
      goto _error;
×
648
    }
649

650
    void* pSinkParam = NULL;
515,159✔
651
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
515,159✔
652
    if (code != TSDB_CODE_SUCCESS) {
514,955!
653
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
×
654
      taosMemoryFree(pSinkManager);
×
655
      goto _error;
×
656
    }
657

658
    SDataSinkNode* pSink = NULL;
514,955✔
659
    if (readHandle->localExec) {
514,955!
660
      code = nodesCloneNode((SNode*)pSubplan->pDataSink, (SNode**)&pSink);
×
661
      if (code != TSDB_CODE_SUCCESS) {
×
662
        qError("failed to nodesCloneNode, srcType:%d, code:%s, %s", nodeType(pSubplan->pDataSink), tstrerror(code),
120!
663
               (*pTask)->id.str);
664
        taosMemoryFree(pSinkManager);
120!
665
        goto _error;
×
666
      }
667
    }
668

669
    // pSinkParam has been freed during create sinker.
670
    code = dsCreateDataSinker(pSinkManager, readHandle->localExec ? &pSink : &pSubplan->pDataSink, handle, pSinkParam,
514,835✔
671
                              (*pTask)->id.str, pSubplan->processOneBlock);
514,835!
672
    if (code) {
514,901✔
673
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
1!
674
    }
675
  }
676

677
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64 " code:%s", taskId, pSubplan->id.queryId,
515,746✔
678
         tstrerror(code));
679

680
_error:
49,362✔
681
  // if failed to add ref for all tables in this query, abort current query
682
  return code;
516,158✔
683
}
684

685
static void freeBlock(void* param) {
×
686
  SSDataBlock* pBlock = *(SSDataBlock**)param;
×
687
  blockDataDestroy(pBlock);
×
688
}
×
689

690
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal,
604,889✔
691
                     bool processOneBlock) {
692
  int32_t        code = TSDB_CODE_SUCCESS;
604,889✔
693
  int32_t        lino = 0;
604,889✔
694
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
604,889✔
695
  int64_t        threadId = taosGetSelfPthreadId();
604,889✔
696

697
  if (pLocal) {
604,896✔
698
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
592,771✔
699
  }
700

701
  taosArrayClear(pResList);
604,896✔
702

703
  int64_t curOwner = 0;
604,846✔
704
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
604,846!
705
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
706
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
707
    return pTaskInfo->code;
×
708
  }
709

710
  if (pTaskInfo->cost.start == 0) {
604,885✔
711
    pTaskInfo->cost.start = taosGetTimestampUs();
515,762✔
712
  }
713

714
  if (isTaskKilled(pTaskInfo)) {
604,812✔
715
    atomic_store_64(&pTaskInfo->owner, 0);
2✔
716
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
2!
717
    return pTaskInfo->code;
2✔
718
  }
719

720
  // error occurs, record the error code and return to client
721
  int32_t ret = setjmp(pTaskInfo->env);
604,866✔
722
  if (ret != TSDB_CODE_SUCCESS) {
605,944✔
723
    pTaskInfo->code = ret;
1,048✔
724
    (void)cleanUpUdfs();
1,048✔
725

726
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
1,048✔
727
    atomic_store_64(&pTaskInfo->owner, 0);
1,048✔
728

729
    return pTaskInfo->code;
1,048✔
730
  }
731

732
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
604,896✔
733

734
  int32_t      current = 0;
604,900✔
735
  SSDataBlock* pRes = NULL;
604,900✔
736
  int64_t      st = taosGetTimestampUs();
604,919✔
737

738
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
604,919!
739
    pTaskInfo->paramSet = true;
49,505✔
740
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
49,505✔
741
  } else {
742
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
555,414✔
743
  }
744

745
  QUERY_CHECK_CODE(code, lino, _end);
603,854!
746
  code = blockDataCheck(pRes);
603,854✔
747
  QUERY_CHECK_CODE(code, lino, _end);
603,855!
748

749
  if (pRes == NULL) {
603,855✔
750
    st = taosGetTimestampUs();
118,715✔
751
  }
752

753
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
603,861✔
754
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
603,861!
755
    rowsThreshold = 4096;
601,375✔
756
  }
757

758
  int32_t blockIndex = 0;
603,861✔
759
  while (pRes != NULL) {
1,440,734✔
760
    SSDataBlock* p = NULL;
920,349✔
761
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
920,349✔
762
      SSDataBlock* p1 = NULL;
728,853✔
763
      code = createOneDataBlock(pRes, true, &p1);
728,853✔
764
      QUERY_CHECK_CODE(code, lino, _end);
728,982!
765

766
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
728,982✔
767
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
728,954!
768
      p = p1;
728,954✔
769
    } else {
770
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
191,561✔
771
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
191,484!
772

773
      p = *(SSDataBlock**)tmp;
191,484✔
774
      code = copyDataBlock(p, pRes);
191,484✔
775
      QUERY_CHECK_CODE(code, lino, _end);
191,893!
776
    }
777

778
    blockIndex += 1;
920,847✔
779

780
    current += p->info.rows;
920,847✔
781
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
920,847!
782
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
783
    void* tmp = taosArrayPush(pResList, &p);
920,803✔
784
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
920,803!
785

786
    if (current >= rowsThreshold || processOneBlock) {
920,803✔
787
      break;
788
    }
789

790
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
837,314✔
791
    QUERY_CHECK_CODE(code, lino, _end);
837,175!
792
    code = blockDataCheck(pRes);
837,175✔
793
    QUERY_CHECK_CODE(code, lino, _end);
836,873!
794
  }
795

796
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
603,874✔
797
    pTaskInfo->pSubplan->rowsThreshold -= current;
2,486✔
798
  }
799

800
  *hasMore = (pRes != NULL);
603,874✔
801
  uint64_t el = (taosGetTimestampUs() - st);
603,861✔
802

803
  pTaskInfo->cost.elapsedTime += el;
603,861✔
804
  if (NULL == pRes) {
603,861✔
805
    *useconds = pTaskInfo->cost.elapsedTime;
520,377✔
806
  }
807

808
_end:
83,484✔
809
  (void)cleanUpUdfs();
603,861✔
810

811
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
603,892✔
812
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
603,892✔
813
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
814

815
  atomic_store_64(&pTaskInfo->owner, 0);
603,892✔
816
  if (code) {
603,891!
817
    pTaskInfo->code = code;
×
818
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
819
  }
820

821
  return pTaskInfo->code;
603,891✔
822
}
823

824
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
×
825
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
826
  SArray*        pList = pTaskInfo->pResultBlockList;
×
827
  size_t         num = taosArrayGetSize(pList);
×
828
  for (int32_t i = 0; i < num; ++i) {
×
829
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
×
830
    if (p) {
×
831
      blockDataDestroy(*p);
×
832
    }
833
  }
834

835
  taosArrayClear(pTaskInfo->pResultBlockList);
×
836
}
×
837

838
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
247,587✔
839
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
247,587✔
840
  int64_t        threadId = taosGetSelfPthreadId();
247,587✔
841
  int64_t        curOwner = 0;
247,584✔
842

843
  *pRes = NULL;
247,584✔
844

845
  // todo extract method
846
  taosRLockLatch(&pTaskInfo->lock);
247,584✔
847
  bool isKilled = isTaskKilled(pTaskInfo);
247,589✔
848
  if (isKilled) {
247,586!
849
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
850

851
    taosRUnLockLatch(&pTaskInfo->lock);
×
852
    return pTaskInfo->code;
×
853
  }
854

855
  if (pTaskInfo->owner != 0) {
247,586!
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

859
    taosRUnLockLatch(&pTaskInfo->lock);
×
860
    return pTaskInfo->code;
×
861
  }
862

863
  pTaskInfo->owner = threadId;
247,586✔
864
  taosRUnLockLatch(&pTaskInfo->lock);
247,586✔
865

866
  if (pTaskInfo->cost.start == 0) {
247,587✔
867
    pTaskInfo->cost.start = taosGetTimestampUs();
680✔
868
  }
869

870
  // error occurs, record the error code and return to client
871
  int32_t ret = setjmp(pTaskInfo->env);
247,587✔
872
  if (ret != TSDB_CODE_SUCCESS) {
247,587!
873
    pTaskInfo->code = ret;
×
874
    (void)cleanUpUdfs();
×
875
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
876
    atomic_store_64(&pTaskInfo->owner, 0);
×
877
    return pTaskInfo->code;
×
878
  }
879

880
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
247,587✔
881

882
  int64_t st = taosGetTimestampUs();
247,588✔
883
  int32_t code = TSDB_CODE_SUCCESS;
247,588✔
884
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
247,588!
885
    pTaskInfo->paramSet = true;
×
886
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, pRes);
×
887
  } else {
888
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, pRes);
247,588✔
889
  }
890
  if (code) {
247,544!
891
    pTaskInfo->code = code;
×
892
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
893
  }
894

895
  code = blockDataCheck(*pRes);
247,544✔
896
  if (code) {
247,562!
897
    pTaskInfo->code = code;
×
898
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
899
  }
900

901
  uint64_t el = (taosGetTimestampUs() - st);
247,551✔
902

903
  pTaskInfo->cost.elapsedTime += el;
247,551✔
904
  if (NULL == *pRes) {
247,551✔
905
    *useconds = pTaskInfo->cost.elapsedTime;
26,409✔
906
  }
907

908
  (void)cleanUpUdfs();
247,551✔
909

910
  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
247,587✔
911
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
247,587✔
912

913
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
247,587!
914
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
915

916
  atomic_store_64(&pTaskInfo->owner, 0);
247,584✔
917
  return pTaskInfo->code;
247,583✔
918
}
919

920
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
186,452✔
921
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
186,452✔
922
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
186,456✔
923
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
186,456✔
924

925
  if (!tmp) {
186,455✔
926
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
1!
927
    return terrno;
1✔
928
  }
929
  return TSDB_CODE_SUCCESS;
186,454✔
930
}
931

932
int32_t stopInfoComp(void const* lp, void const* rp) {
×
933
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
×
934
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;
×
935

936
  if (key->refId < pInfo->refId) {
×
937
    return -1;
×
938
  } else if (key->refId > pInfo->refId) {
×
939
    return 1;
×
940
  }
941

942
  return 0;
×
943
}
944

945
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
×
946
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
947
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
×
948
  if (idx >= 0) {
×
949
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
×
950
  }
951
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
952
}
×
953

954
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
1,035✔
955
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
1,035✔
956

957
  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
1,035✔
958
  for (int32_t i = 0; i < num; ++i) {
1,048✔
959
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
13✔
960
    if (!pStop) {
13!
961
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
962
      continue;
×
963
    }
964
    SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
13✔
965
    if (pExchangeInfo) {
13!
966
      int32_t code = tsem_post(&pExchangeInfo->ready);
13✔
967
      if (code != TSDB_CODE_SUCCESS) {
13!
968
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
969
      } else {
970
        qDebug("post to exchange %" PRId64 " to stop", pStop->refId);
13✔
971
      }
972
      code = taosReleaseRef(exchangeObjRefPool, pStop->refId);
13✔
973
      if (code != TSDB_CODE_SUCCESS) {
13!
974
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
975
      }
976
    }
977
  }
978

979
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
1,035✔
980
}
1,035✔
981

982
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
1,035✔
983
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
1,035✔
984
  if (pTaskInfo == NULL) {
1,035!
985
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
986
  }
987

988
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
1,035✔
989

990
  setTaskKilled(pTaskInfo, rspCode);
1,035✔
991
  qStopTaskOperators(pTaskInfo);
1,035✔
992

993
  return TSDB_CODE_SUCCESS;
1,035✔
994
}
995

996
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode, int64_t waitDuration) {
×
997
  int64_t        st = taosGetTimestampMs();
×
998
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
999
  if (pTaskInfo == NULL) {
×
1000
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
1001
  }
1002

1003
  if (waitDuration > 0) {
×
1004
    qDebug("%s sync killed execTask, and waiting for at most %.2fs", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1005
  } else {
1006
    qDebug("%s async killed execTask", GET_TASKID(pTaskInfo));
×
1007
  }
1008

1009
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1010

1011
  if (waitDuration > 0) {
×
1012
    while (1) {
1013
      taosWLockLatch(&pTaskInfo->lock);
×
1014
      if (qTaskIsExecuting(pTaskInfo)) {  // let's wait for 100 ms and try again
×
1015
        taosWUnLockLatch(&pTaskInfo->lock);
×
1016

1017
        taosMsleep(200);
×
1018

1019
        int64_t d = taosGetTimestampMs() - st;
×
1020
        if (d >= waitDuration && waitDuration >= 0) {
×
1021
          qWarn("%s waiting more than %.2fs, not wait anymore", GET_TASKID(pTaskInfo), waitDuration / 1000.0);
×
1022
          return TSDB_CODE_SUCCESS;
×
1023
        }
1024
      } else {  // not running now
1025
        pTaskInfo->code = rspCode;
×
1026
        taosWUnLockLatch(&pTaskInfo->lock);
×
1027
        return TSDB_CODE_SUCCESS;
×
1028
      }
1029
    }
1030
  }
1031

1032
  int64_t et = taosGetTimestampMs() - st;
×
1033
  if (et < waitDuration) {
×
1034
    qInfo("%s  waiting %.2fs for executor stopping", GET_TASKID(pTaskInfo), et / 1000.0);
×
1035
    return TSDB_CODE_SUCCESS;
×
1036
  }
1037
  return TSDB_CODE_SUCCESS;
×
1038
}
1039

1040
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
×
1041
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
1042
  if (NULL == pTaskInfo) {
×
1043
    return false;
×
1044
  }
1045

1046
  return 0 != atomic_load_64(&pTaskInfo->owner);
×
1047
}
1048

1049
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
529,217✔
1050
  STaskCostInfo* pSummary = &pTaskInfo->cost;
529,217✔
1051
  int64_t        idleTime = pSummary->start - pSummary->created;
529,217✔
1052

1053
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
529,217✔
1054
  if (pSummary->pRecoder != NULL) {
529,217✔
1055
    qDebug(
378,515✔
1056
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
1057
        "createGroupIdMap:%.2f ms, total blocks:%d, "
1058
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
1059
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
1060
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
1061
        pRecorder->totalRows, pRecorder->totalCheckedRows);
1062
  } else {
1063
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
150,702✔
1064
           pSummary->elapsedTime / 1000.0);
1065
  }
1066
}
529,216✔
1067

1068
void qDestroyTask(qTaskInfo_t qTaskHandle) {
538,313✔
1069
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
538,313✔
1070
  if (pTaskInfo == NULL) {
538,313✔
1071
    return;
9,098✔
1072
  }
1073

1074
  if (pTaskInfo->pRoot != NULL) {
529,215!
1075
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
529,216✔
1076
  } else {
1077
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1078
  }
1079

1080
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
529,218✔
1081
  doDestroyTask(pTaskInfo);
529,217✔
1082
}
1083

1084
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
8,328✔
1085
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
8,328✔
1086
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
8,328✔
1087
}
1088

1089
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
961✔
1090
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
961✔
1091
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
961✔
1092

1093
  while (1) {
938✔
1094
    uint16_t type = pOperator->operatorType;
1,899✔
1095
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
1,899✔
1096
      *scanner = pOperator->info;
961✔
1097
      break;
961✔
1098
    } else {
1099
      pOperator = pOperator->pDownstream[0];
938✔
1100
    }
1101
  }
1102
}
961✔
1103

1104
static int32_t getOpratorIntervalInfo(SOperatorInfo* pOperator, int64_t* pWaterMark, SInterval* pInterval,
×
1105
                                      STimeWindow* pLastWindow, TSKEY* pRecInteral) {
1106
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1107
    return getOpratorIntervalInfo(pOperator->pDownstream[0], pWaterMark, pInterval, pLastWindow, pRecInteral);
×
1108
  }
1109
  SStreamScanInfo* pScanOp = (SStreamScanInfo*)pOperator->info;
×
1110
  *pWaterMark = pScanOp->twAggSup.waterMark;
×
1111
  *pInterval = pScanOp->interval;
×
1112
  *pLastWindow = pScanOp->lastScanRange;
×
1113
  *pRecInteral = pScanOp->recalculateInterval;
×
1114
  return TSDB_CODE_SUCCESS;
×
1115
}
1116

1117
void* qExtractReaderFromTmqScanner(void* scanner) {
961✔
1118
  SStreamScanInfo* pInfo = scanner;
961✔
1119
  return (void*)pInfo->tqReader;
961✔
1120
}
1121

1122
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
2,532✔
1123
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,532✔
1124
  return pTaskInfo->streamInfo.schema;
2,532✔
1125
}
1126

1127
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
2,532✔
1128
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,532✔
1129
  return pTaskInfo->streamInfo.tbName;
2,532✔
1130
}
1131

1132
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
2,639✔
1133
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,639✔
1134
  return &pTaskInfo->streamInfo.btMetaRsp;
2,639✔
1135
}
1136

1137
int32_t qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
28,979✔
1138
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
28,979✔
1139
  tOffsetCopy(pOffset, &pTaskInfo->streamInfo.currentOffset);
28,979✔
1140
  return 0;
28,979✔
1141
  /*if (code != TSDB_CODE_SUCCESS) {
1142
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1143
    pTaskInfo->code = code;
1144
    T_LONG_JMP(pTaskInfo->env, code);
1145
  }*/
1146
}
1147

1148
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
2,513✔
1149
  memset(pCond, 0, sizeof(SQueryTableDataCond));
2,513✔
1150
  pCond->order = TSDB_ORDER_ASC;
2,513✔
1151
  pCond->numOfCols = pMtInfo->schema->nCols;
2,513✔
1152
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
2,513!
1153
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
2,512!
1154
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
2,512!
1155
    taosMemoryFreeClear(pCond->colList);
×
1156
    taosMemoryFreeClear(pCond->pSlotList);
×
1157
    return terrno;
×
1158
  }
1159

1160
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
2,512✔
1161
  pCond->suid = pMtInfo->suid;
2,512✔
1162
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
2,512✔
1163
  pCond->startVersion = -1;
2,512✔
1164
  pCond->endVersion = sContext->snapVersion;
2,512✔
1165

1166
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
15,818✔
1167
    SColumnInfo* pColInfo = &pCond->colList[i];
13,306✔
1168
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
13,306✔
1169
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
13,306✔
1170
    if (pMtInfo->pExtSchemas != NULL) {
13,306✔
1171
      decimalFromTypeMod(pMtInfo->pExtSchemas[i].typeMod, &pColInfo->precision, &pColInfo->scale);
162✔
1172
    }
1173
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
13,306✔
1174
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
13,306✔
1175

1176
    pCond->pSlotList[i] = i;
13,306✔
1177
  }
1178

1179
  return TSDB_CODE_SUCCESS;
2,512✔
1180
}
1181

1182
void qStreamSetOpen(qTaskInfo_t tinfo) {
242,506✔
1183
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
242,506✔
1184
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
242,506✔
1185
  pOperator->status = OP_NOT_OPENED;
242,506✔
1186
}
242,506✔
1187

1188
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
26,279✔
1189
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
26,279✔
1190
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
26,279✔
1191
}
26,279✔
1192

1193
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
29,610✔
1194
  int32_t        code = TSDB_CODE_SUCCESS;
29,610✔
1195
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
29,610✔
1196
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
29,610✔
1197

1198
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
29,610✔
1199
  const char*    id = GET_TASKID(pTaskInfo);
29,610✔
1200

1201
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
29,610✔
1202
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
25,466✔
1203
    if (pOperator == NULL || code != 0) {
25,466!
1204
      return code;
×
1205
    }
1206

1207
    SStreamScanInfo* pInfo = pOperator->info;
25,466✔
1208
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
25,466✔
1209
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
25,466✔
1210
    walReaderVerifyOffset(pWalReader, pOffset);
25,466✔
1211
  }
1212
  // if pOffset equal to current offset, means continue consume
1213
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
29,610✔
1214
    return 0;
24,977✔
1215
  }
1216

1217
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
4,632✔
1218
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
2,065✔
1219
    if (pOperator == NULL || code != 0) {
2,065!
1220
      return code;
×
1221
    }
1222

1223
    SStreamScanInfo* pInfo = pOperator->info;
2,065✔
1224
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
2,065✔
1225
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
2,065✔
1226
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
2,065✔
1227

1228
    if (pOffset->type == TMQ_OFFSET__LOG) {
2,065✔
1229
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,305✔
1230
      pScanBaseInfo->dataReader = NULL;
1,305✔
1231

1232
      SStoreTqReader* pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
1,305✔
1233
      SWalReader*     pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
1,305✔
1234
      walReaderVerifyOffset(pWalReader, pOffset);
1,305✔
1235
      code = pReaderAPI->tqReaderSeek(pInfo->tqReader, pOffset->version, id);
1,305✔
1236
      if (code < 0) {
1,305✔
1237
        qError("tqReaderSeek failed ver:%" PRId64 ", %s", pOffset->version, id);
18!
1238
        return code;
18✔
1239
      }
1240
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
760!
1241
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
1242
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1243
      int64_t uid = pOffset->uid;
760✔
1244
      int64_t ts = pOffset->ts;
760✔
1245
      int32_t index = 0;
760✔
1246

1247
      // this value may be changed if new tables are created
1248
      taosRLockLatch(&pTaskInfo->lock);
760✔
1249
      int32_t numOfTables = 0;
760✔
1250
      code = tableListGetSize(pTableListInfo, &numOfTables);
760✔
1251
      if (code != TSDB_CODE_SUCCESS) {
760!
1252
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1253
        taosRUnLockLatch(&pTaskInfo->lock);
×
1254
        return code;
625✔
1255
      }
1256

1257
      if (uid == 0) {
760✔
1258
        if (numOfTables != 0) {
739✔
1259
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
114✔
1260
          if (!tmp) {
114!
1261
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1262
            taosRUnLockLatch(&pTaskInfo->lock);
×
1263
            return terrno;
×
1264
          }
1265
          if (tmp) uid = tmp->uid;
114!
1266
          ts = INT64_MIN;
114✔
1267
          pScanInfo->currentTable = 0;
114✔
1268
        } else {
1269
          taosRUnLockLatch(&pTaskInfo->lock);
625✔
1270
          qError("no table in table list, %s", id);
625!
1271
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
625✔
1272
        }
1273
      }
1274
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
135✔
1275

1276
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
134!
1277
             pInfo->pTableScanOp->resultInfo.totalRows);
1278
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
134✔
1279

1280
      // start from current accessed position
1281
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
1282
      // position, let's find it from the beginning.
1283
      index = tableListFind(pTableListInfo, uid, 0);
134✔
1284
      taosRUnLockLatch(&pTaskInfo->lock);
135✔
1285

1286
      if (index >= 0) {
135!
1287
        pScanInfo->currentTable = index;
135✔
1288
      } else {
1289
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
×
1290
               numOfTables, pScanInfo->currentTable, id);
1291
        return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
×
1292
      }
1293

1294
      STableKeyInfo keyInfo = {.uid = uid};
135✔
1295
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
135✔
1296

1297
      // let's start from the next ts that returned to consumer.
1298
      if (pTaskInfo->storageAPI.tqReaderFn.tqGetTablePrimaryKey(pInfo->tqReader)) {
135!
1299
        pScanBaseInfo->cond.twindows.skey = ts;
×
1300
      } else {
1301
        pScanBaseInfo->cond.twindows.skey = ts + 1;
135✔
1302
      }
1303
      pScanInfo->scanTimes = 0;
135✔
1304

1305
      if (pScanBaseInfo->dataReader == NULL) {
135✔
1306
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond,
114✔
1307
                                                             &keyInfo, 1, pScanInfo->pResBlock,
1308
                                                             (void**)&pScanBaseInfo->dataReader, id, NULL);
114✔
1309
        if (code != TSDB_CODE_SUCCESS) {
113!
1310
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
×
1311
          return code;
×
1312
        }
1313

1314
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
113✔
1315
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1316
      } else {
1317
        code = pTaskInfo->storageAPI.tsdReader.tsdSetQueryTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
21✔
1318
        if (code != TSDB_CODE_SUCCESS) {
21!
1319
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1320
          return code;
×
1321
        }
1322

1323
        code = pTaskInfo->storageAPI.tsdReader.tsdReaderResetStatus(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
21✔
1324
        if (code != TSDB_CODE_SUCCESS) {
21!
1325
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1326
          return code;
×
1327
        }
1328
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
21!
1329
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1330
      }
1331

1332
      // restore the key value
1333
      pScanBaseInfo->cond.twindows.skey = oldSkey;
135✔
1334
    } else {
1335
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1336
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1337
    }
1338

1339
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1340
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
2,567✔
1341
      SStreamRawScanInfo* pInfo = pOperator->info;
2,518✔
1342
      SSnapContext*       sContext = pInfo->sContext;
2,518✔
1343
      SOperatorInfo*      p = NULL;
2,518✔
1344

1345
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
2,518✔
1346
      if (code != 0) {
2,518!
1347
        return code;
×
1348
      }
1349

1350
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
2,518✔
1351

1352
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
2,518!
1353
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1354
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1355
      }
1356

1357
      SMetaTableInfo mtInfo = {0};
2,518✔
1358
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
2,518✔
1359
      if (code != 0) {
2,518!
1360
        destroyMetaTableInfo(&mtInfo);
1361
        return code;
×
1362
      }
1363
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,518✔
1364
      pInfo->dataReader = NULL;
2,517✔
1365

1366
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,517✔
1367
      tableListClear(pTableListInfo);
2,517✔
1368

1369
      if (mtInfo.uid == 0) {
2,518✔
1370
        destroyMetaTableInfo(&mtInfo);
1371
        goto end;  // no data
4✔
1372
      }
1373

1374
      pAPI->snapshotFn.taosXSetTablePrimaryKey(sContext, mtInfo.uid);
2,514✔
1375
      code = initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
2,513✔
1376
      if (code != TSDB_CODE_SUCCESS) {
2,514!
1377
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1378
        destroyMetaTableInfo(&mtInfo);
1379
        return code;
×
1380
      }
1381
      if (pAPI->snapshotFn.taosXGetTablePrimaryKey(sContext)) {
2,514!
1382
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
×
1383
      } else {
1384
        pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts + 1;
2,513✔
1385
      }
1386

1387
      code = tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
2,513✔
1388
      if (code != TSDB_CODE_SUCCESS) {
2,514!
1389
        destroyMetaTableInfo(&mtInfo);
1390
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1391
        return code;
×
1392
      }
1393

1394
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
2,514✔
1395
      if (!pList) {
2,514!
1396
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1397
        destroyMetaTableInfo(&mtInfo);
1398
        return code;
×
1399
      }
1400
      int32_t size = 0;
2,514✔
1401
      code = tableListGetSize(pTableListInfo, &size);
2,514✔
1402
      if (code != TSDB_CODE_SUCCESS) {
2,514!
1403
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1404
        destroyMetaTableInfo(&mtInfo);
1405
        return code;
×
1406
      }
1407

1408
      code = pTaskInfo->storageAPI.tsdReader.tsdReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size,
2,514✔
1409
                                                           NULL, (void**)&pInfo->dataReader, NULL, NULL);
2,514✔
1410
      if (code != TSDB_CODE_SUCCESS) {
2,513!
1411
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1412
        destroyMetaTableInfo(&mtInfo);
1413
        return code;
×
1414
      }
1415

1416
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,513✔
1417
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
2,513✔
1418
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1419
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
2,513✔
1420
      pTaskInfo->streamInfo.schema = mtInfo.schema;
2,514✔
1421
      taosMemoryFreeClear(mtInfo.pExtSchemas);
2,514!
1422

1423
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
2,514!
1424
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
49✔
1425
      SStreamRawScanInfo* pInfo = pOperator->info;
18✔
1426
      SSnapContext*       sContext = pInfo->sContext;
18✔
1427
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
18✔
1428
      if (code != 0) {
18!
1429
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1430
        return code;
×
1431
      }
1432
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
18!
1433
             id);
1434
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
31!
1435
      SStreamRawScanInfo* pInfo = pOperator->info;
31✔
1436
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
31✔
1437
      pInfo->dataReader = NULL;
31✔
1438
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
31!
1439
    }
1440
  }
1441

1442
end:
×
1443
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
3,989✔
1444
  return 0;
3,989✔
1445
}
1446

1447
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
382,662✔
1448
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
382,662✔
1449
  if (pMsg->info.ahandle == NULL) {
382,662!
1450
    qError("pMsg->info.ahandle is NULL");
×
1451
    return;
×
1452
  }
1453

1454
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
382,662✔
1455
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1456

1457
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
382,663✔
1458

1459
  if (pMsg->contLen > 0) {
382,663✔
1460
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
382,638!
1461
    if (buf.pData == NULL) {
382,621!
1462
      pMsg->code = terrno;
×
1463
    } else {
1464
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
382,621✔
1465
    }
1466
  }
1467

1468
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
382,646✔
1469
  rpcFreeCont(pMsg->pCont);
382,709✔
1470
  destroySendMsgInfo(pSendInfo);
382,694✔
1471
}
1472

1473
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1474
  int32_t        code = TSDB_CODE_SUCCESS;
×
1475
  int32_t        lino = 0;
×
1476
  SExecTaskInfo* pTaskInfo = tinfo;
×
1477
  SArray*        plist = NULL;
×
1478

1479
  code = getTableListInfo(pTaskInfo, &plist);
×
1480
  if (code || plist == NULL) {
×
1481
    return NULL;
×
1482
  }
1483

1484
  // only extract table in the first elements
1485
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1486

1487
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1488
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1489

1490
  int32_t numOfTables = 0;
×
1491
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1492
  QUERY_CHECK_CODE(code, lino, _end);
×
1493

1494
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1495
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1496
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1497
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1498
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1499
  }
1500

1501
  taosArrayDestroy(plist);
×
1502

1503
_end:
×
1504
  if (code != TSDB_CODE_SUCCESS) {
×
1505
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1506
    T_LONG_JMP(pTaskInfo->env, code);
×
1507
  }
1508
  return pUidList;
×
1509
}
1510

1511
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
11,148✔
1512
  int32_t        code = TSDB_CODE_SUCCESS;
11,148✔
1513
  int32_t        lino = 0;
11,148✔
1514
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
11,148✔
1515

1516
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
11,148!
1517
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1518
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1519

1520
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1521
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1522
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
11,148✔
1523
    STableScanInfo* pScanInfo = pOperator->info;
5,574✔
1524

1525
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
5,574✔
1526
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
5,574!
1527
  } else {
1528
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
5,574!
1529
      code = extractTableList(pList, pOperator->pDownstream[0]);
5,574✔
1530
    }
1531
  }
1532

1533
_end:
×
1534
  if (code != TSDB_CODE_SUCCESS) {
11,148!
1535
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1536
  }
1537
  return code;
11,148✔
1538
}
1539

1540
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
5,574✔
1541
  if (pList == NULL) {
5,574!
1542
    return TSDB_CODE_INVALID_PARA;
×
1543
  }
1544

1545
  *pList = NULL;
5,574✔
1546
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
5,574✔
1547
  if (pArray == NULL) {
5,574!
1548
    return terrno;
×
1549
  }
1550

1551
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
5,574✔
1552
  if (code == 0) {
5,574!
1553
    *pList = pArray;
5,574✔
1554
  } else {
1555
    taosArrayDestroy(pArray);
×
1556
  }
1557
  return code;
5,574✔
1558
}
1559

1560
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1561
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1562
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1563
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1564
  }
1565
  return 0;
×
1566
}
1567

1568
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1569
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1570
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1571
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1572
  }
1573
  return 0;
×
1574
}
1575

1576
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1577
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1578

1579
  int32_t code = pTaskInfo->code;
×
1580
  pTaskInfo->code = 0;
×
1581
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1582
}
×
1583

1584
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1585
  int32_t code = 0;
×
1586
  return code;
×
1587
}
1588

1589
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1590
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1591
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1592
  return code;
×
1593
}
1594

1595
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
119,305✔
1596
  int32_t code = 0;
119,305✔
1597

1598
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
119,305✔
1599
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
119,300✔
1600

1601
  if (pOper->fpSet.resetStateFn) {
119,297!
1602
    code = pOper->fpSet.resetStateFn(pOper);
119,297✔
1603
  }
1604
  pOper->status = OP_NOT_OPENED;
119,298✔
1605
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
207,208!
1606
    code = clearStatesForOperator(pOper->pDownstream[i]);
87,903✔
1607
  }
1608
  return code;
119,305✔
1609
}
1610

1611
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
31,406✔
1612
  int32_t        code = 0;
31,406✔
1613
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
31,406✔
1614
  SOperatorInfo* pOper = pTaskInfo->pRoot;
31,406✔
1615
  pTaskInfo->code = TSDB_CODE_SUCCESS;
31,406✔
1616
  code = clearStatesForOperator(pOper);
31,406✔
1617
  return code;
31,406✔
1618
}
1619

1620
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
39,110✔
1621
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
39,110✔
1622
  int64_t        threadId = taosGetSelfPthreadId();
39,110✔
1623
  int64_t        curOwner = 0;
39,110✔
1624

1625
  *ppRes = NULL;
39,110✔
1626

1627
  // todo extract method
1628
  taosRLockLatch(&pTaskInfo->lock);
39,110✔
1629
  bool isKilled = isTaskKilled(pTaskInfo);
39,109✔
1630
  if (isKilled) {
39,109!
1631
    // clearStreamBlock(pTaskInfo->pRoot);
1632
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1633

1634
    taosRUnLockLatch(&pTaskInfo->lock);
×
1635
    return pTaskInfo->code;
×
1636
  }
1637

1638
  if (pTaskInfo->owner != 0) {
39,109!
1639
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1640
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1641

1642
    taosRUnLockLatch(&pTaskInfo->lock);
×
1643
    return pTaskInfo->code;
×
1644
  }
1645

1646
  pTaskInfo->owner = threadId;
39,109✔
1647
  taosRUnLockLatch(&pTaskInfo->lock);
39,109✔
1648

1649
  if (pTaskInfo->cost.start == 0) {
39,111✔
1650
    pTaskInfo->cost.start = taosGetTimestampUs();
660✔
1651
  }
1652

1653
  // error occurs, record the error code and return to client
1654
  int32_t ret = setjmp(pTaskInfo->env);
39,111✔
1655
  if (ret != TSDB_CODE_SUCCESS) {
51,275✔
1656
    pTaskInfo->code = ret;
12,166✔
1657
    (void)cleanUpUdfs();
12,166✔
1658
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
12,166✔
1659
    atomic_store_64(&pTaskInfo->owner, 0);
12,166✔
1660
    return pTaskInfo->code;
12,166✔
1661
  }
1662

1663
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
39,109✔
1664

1665
  int64_t st = taosGetTimestampUs();
39,111✔
1666

1667
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
39,111✔
1668
  if (code) {
26,944!
1669
    pTaskInfo->code = code;
×
1670
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1671
  } else {
1672
    *finished = *ppRes == NULL;
26,944✔
1673
    code = blockDataCheck(*ppRes);
26,944✔
1674
  }
1675
  if (code) {
26,944!
1676
    pTaskInfo->code = code;
×
1677
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1678
  }
1679

1680
  uint64_t el = (taosGetTimestampUs() - st);
26,944✔
1681

1682
  pTaskInfo->cost.elapsedTime += el;
26,944✔
1683
  if (NULL == *ppRes) {
26,944✔
1684
    *useconds = pTaskInfo->cost.elapsedTime;
18,720✔
1685
  }
1686

1687
  (void)cleanUpUdfs();
26,944✔
1688

1689
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
26,945✔
1690
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
26,945✔
1691

1692
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
26,945✔
1693
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1694

1695
  atomic_store_64(&pTaskInfo->owner, 0);
26,945✔
1696
  return pTaskInfo->code;
26,945✔
1697
}
1698

1699
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1700
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1701
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1702
// }
1703

1704
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
3,330✔
1705
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1706
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1707
  STableListInfo* pList = tableListCreate();
3,330✔
1708
  if (pList == NULL) {
3,327!
1709
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1710
    return terrno;
×
1711
  }
1712

1713
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
3,327✔
1714
  SReadHandle    pHandle = {.vnode = pVnode};
3,327✔
1715
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
3,327✔
1716

1717
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
3,327✔
1718
  if (code != 0) {
3,330!
1719
    tableListDestroy(pList);
×
1720
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1721
    return code;
×
1722
  }
1723
  *pTableListInfo = pList;
3,330✔
1724
  return 0;
3,330✔
1725
}
1726

1727
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
30,654✔
1728
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
30,654!
1729
    return TSDB_CODE_INVALID_PARA;
×
1730
  }
1731
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
30,654✔
1732
    *size = 0;
22,825✔
1733
    *pKeyInfo = NULL;
22,825✔
1734
    return 0;
22,825✔
1735
  }
1736
  if (currentGroupId == -1) {
7,828✔
1737
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
693✔
1738
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
693✔
1739
    return 0;
693✔
1740
  }
1741
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
7,135✔
1742
}
1743

1744
int32_t  qStreamSetTableList(void** pTableListInfo, uint64_t uid, uint64_t gid){
2,689✔
1745
  if (*pTableListInfo == NULL) {
2,689✔
1746
    *pTableListInfo = tableListCreate();
773✔
1747
    if (*pTableListInfo == NULL) {
773!
1748
      return terrno;
×
1749
    }
1750
  }
1751
  return tableListAddTableInfo(*pTableListInfo, uid, gid);
2,689✔
1752
}
1753

1754
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
27,805✔
1755
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
27,805✔
1756
    return 0;
22,685✔
1757
  }
1758
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
5,898✔
1759
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
5,755✔
1760

1761
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
5,755✔
1762
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
5,755!
1763
      return i;
4,977✔
1764
    }
1765
  }
1766
  return -1;
143✔
1767
}
1768

1769
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
4,297✔
1770

1771
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
13,833✔
1772

1773
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
2,394✔
1774
void    qStreamSetTableListGroupNum(const void* pTableList, int32_t groupNum) {((STableListInfo*)pTableList)->numOfOuputGroups = groupNum; }
773✔
1775
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
492✔
1776

1777
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo, SColumnInfoData** pRet) { return doFilter(pBlock, pFilterInfo, NULL, pRet); }
3,675✔
1778

1779
void streamDestroyExecTask(qTaskInfo_t tInfo) {
5,415✔
1780
  qDebug("streamDestroyExecTask called, task:%p", tInfo);
5,415✔
1781
  qDestroyTask(tInfo);
5,415✔
1782
}
5,415✔
1783

1784
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
1,663✔
1785
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
1,663✔
1786
}
1787

1788
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx,
1,664✔
1789
                                       const SStreamRuntimeFuncInfo* pExtraParams) {
1790
  int32_t      code = 0;
1,664✔
1791
  SNode*       pNode = 0;
1,664✔
1792
  SNodeList*   pList = NULL;
1,664✔
1793
  SExprInfo*   pExprInfo = NULL;
1,664✔
1794
  int32_t      numOfExprs = 1;
1,664✔
1795
  int32_t*     offset = 0;
1,664✔
1796
  STargetNode* pTargetNode = NULL;
1,664✔
1797
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
1,664✔
1798
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
1,665!
1799

1800
  if (code == 0) {
1,665!
1801
    pTargetNode->dataBlockId = 0;
1,665✔
1802
    pTargetNode->pExpr = pNode;
1,665✔
1803
    pTargetNode->slotId = 0;
1,665✔
1804
  }
1805
  if (code == 0) {
1,665!
1806
    code = nodesMakeList(&pList);
1,665✔
1807
  }
1808
  if (code == 0) {
1,665!
1809
    code = nodesListAppend(pList, (SNode*)pTargetNode);
1,665✔
1810
  }
1811
  if (code == 0) {
1,666!
1812
    pNode = NULL;
1,666✔
1813
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
1,666✔
1814
  }
1815

1816
  if (code == 0) {
1,665!
1817
    const char* pVal = NULL;
1,665✔
1818
    int32_t     len = 0;
1,665✔
1819
    SNode*      pSclNode = NULL;
1,665✔
1820
    switch (pExprInfo->pExpr->nodeType) {
1,665!
1821
      case QUERY_NODE_FUNCTION:
1,665✔
1822
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
1,665✔
1823
        break;
1,665✔
1824
      case QUERY_NODE_OPERATOR:
×
1825
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1826
        break;
×
1827
      default:
×
1828
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1829
        break;
×
1830
    }
1831
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
1,665✔
1832
    SSDataBlock block = {0};
1,666✔
1833
    block.info.rows = 1;
1,666✔
1834
    SSDataBlock* pBlock = &block;
1,666✔
1835
    void*        tmp = taosArrayPush(pBlockList, &pBlock);
1,665✔
1836
    if (tmp == NULL) {
1,665!
1837
      code = terrno;
×
1838
    }
1839
    if (code == 0) {
1,665!
1840
      code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
1,665✔
1841
    }
1842
    taosArrayDestroy(pBlockList);
1,664✔
1843
  }
1844
  nodesDestroyList(pList);
1,664✔
1845
  destroyExprInfo(pExprInfo, numOfExprs);
1,666✔
1846
  taosMemoryFreeClear(pExprInfo);
1,667!
1847
  return code;
1,666✔
1848
}
1849

1850
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
10,492✔
1851
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
10,492✔
1852
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
10,492✔
1853
  int32_t        code = 0;
10,492✔
1854
  SNode*         pNode = NULL;
10,492✔
1855
  if (!pForceOutputCols) return 0;
10,492✔
1856
  if (!*pRes) {
1!
1857
    code = createDataBlock(pRes);
1✔
1858
  }
1859

1860
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
1!
1861
    int32_t idx = 0;
1✔
1862
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
3✔
1863
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
2✔
1864
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
2✔
1865
      colInfo.info.precision = pCol->type.precision;
2✔
1866
      colInfo.info.scale = pCol->type.scale;
2✔
1867
      code = blockDataAppendColInfo(*pRes, &colInfo);
2✔
1868
      if (code != 0) break;
2!
1869
    }
1870
  }
1871

1872
  code = blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
1✔
1873
  if (code != TSDB_CODE_SUCCESS) {
1!
1874
    qError("failed to ensure capacity for force output, code:%s", tstrerror(code));
×
1875
    return code;
×
1876
  }
1877

1878
  // loop all exprs for force output, execute all exprs
1879
  int32_t idx = 0;
1✔
1880
  int32_t rowIdx = (*pRes)->info.rows;
1✔
1881
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
1✔
1882
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
1✔
1883
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
3✔
1884
    SScalarParam   dst = {0};
2✔
1885
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
2✔
1886
    code = nodesStringToNode(pCol->expr, &pNode);
2✔
1887
    if (code != 0) break;
2!
1888
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
2✔
1889
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
2✔
1890
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
1✔
1891
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
1✔
1892
    } else {
1893
      dst.columnData = pInfo;
1✔
1894
      dst.numOfRows = rowIdx;
1✔
1895
      dst.colAlloced = false;
1✔
1896
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx, rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
1✔
1897
    }
1898
    ++idx;
2✔
1899
    // TODO sclFreeParam(&dst);
1900
    nodesDestroyNode(pNode);
2✔
1901
    if (code != 0) break;
2!
1902
  }
1903
  if (code == TSDB_CODE_SUCCESS) {
1!
1904
    (*pRes)->info.rows++;
1✔
1905
  }
1906
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
1✔
1907
  return code;
1✔
1908
}
1909

1910
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
607✔
1911
  int32_t      code = 0;
607✔
1912
  const char*  pVal = NULL;
607✔
1913
  SScalarParam dst = {0};
607✔
1914
  int32_t      len = 0;
607✔
1915
  int32_t      nextIdx = pStreamRuntimeInfo->curIdx;
607✔
1916
  pStreamRuntimeInfo->curIdx = 0;  // always use the first window to calc tbname
607✔
1917
  // execute the expr
1918
  switch (pExpr->type) {
607!
1919
    case QUERY_NODE_VALUE: {
×
1920
      SValueNode* pValue = (SValueNode*)pExpr;
×
1921
      int32_t     type = pValue->node.resType.type;
×
1922
      if (!IS_STR_DATA_TYPE(type)) {
×
1923
        qError("invalid sub tb expr with non-str type");
×
1924
        code = TSDB_CODE_INVALID_PARA;
×
1925
        break;
×
1926
      }
1927
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
1928
      if (pTmp == NULL) {
×
1929
        qError("invalid sub tb expr with null value");
×
1930
        code = TSDB_CODE_INVALID_PARA;
×
1931
        break;
×
1932
      }
1933
      pVal = varDataVal(pTmp);
×
1934
      len = varDataLen(pTmp);
×
1935
    } break;
×
1936
    case QUERY_NODE_FUNCTION: {
607✔
1937
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
607✔
1938
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
607!
1939
        qError("invalid sub tb expr with non-str type func");
×
1940
        code = TSDB_CODE_INVALID_PARA;
×
1941
        break;
×
1942
      }
1943
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
607!
1944
      if (!pCol) {
607!
1945
        code = terrno;
×
1946
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
1947
        break;
×
1948
      }
1949

1950
      pCol->hasNull = true;
607✔
1951
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
607✔
1952
      pCol->info.colId = 0;
607✔
1953
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
607✔
1954
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
607✔
1955
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
607✔
1956
      code = colInfoDataEnsureCapacity(pCol, 1, true);
607✔
1957
      if (code != 0) {
606!
1958
        qError("failed to ensure capacity for col info data at: %s, %d", __func__, __LINE__);
×
1959
        taosMemoryFree(pCol);
×
1960
        break;
×
1961
      }
1962
      dst.columnData = pCol;
606✔
1963
      dst.numOfRows = 1;
606✔
1964
      dst.colAlloced = true;
606✔
1965
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
606✔
1966
      if (colDataIsNull_var(dst.columnData, 0)) {
607!
1967
        qInfo("invalid sub tb expr with null value");
×
1968
        code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED;
×
1969
      }
1970
      if (code == 0) {
607!
1971
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
607✔
1972
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
607✔
1973
      }
1974
    } break;
607✔
1975
    default:
×
1976
      qError("wrong subtable expr with type: %d", pExpr->type);
×
1977
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1978
      break;
×
1979
  }
1980
  if (code == 0) {
607!
1981
    if (!pVal || len == 0) {
607!
1982
      qError("tbname generated with no characters which is not allowed");
×
1983
      code = TSDB_CODE_INVALID_PARA;
×
1984
    }
1985
    if(len > TSDB_TABLE_NAME_LEN - 1) {
607✔
1986
      qError("tbname generated with too long characters, max allowed is %d, got %d, truncated.", TSDB_TABLE_NAME_LEN - 1, len);
2!
1987
      len = TSDB_TABLE_NAME_LEN - 1;
2✔
1988
    }
1989

1990
    memcpy(tbname, pVal, len);
607✔
1991
    tbname[len] = '\0';  // ensure null terminated
607✔
1992
    if (NULL != strchr(tbname, '.')) {
607!
1993
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
1994
      qError("tbname generated with invalid characters, '.' is not allowed");
×
1995
    }
1996
  }
1997
  // TODO free dst
1998
  sclFreeParam(&dst);
607✔
1999
  pStreamRuntimeInfo->curIdx = nextIdx; // restore
607✔
2000
  return code;
607✔
2001
}
2002

2003
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
644✔
2004
  if (pParam) {
644!
2005
    if (pParam->tbname) {
644!
2006
      taosMemFree(pParam->tbname);
644✔
2007
      pParam->tbname = NULL;
644✔
2008
    }
2009
    if (pParam->stbname) {
644!
2010
      taosMemFree(pParam->stbname);
644✔
2011
      pParam->stbname = NULL;
644✔
2012
    }
2013
    if (pParam->dbFName) {
644!
2014
      taosMemFree(pParam->dbFName);
644✔
2015
      pParam->dbFName = NULL;
644✔
2016
    }
2017
    if (pParam->pFields) {
644!
2018
      taosArrayDestroy(pParam->pFields);
644✔
2019
      pParam->pFields = NULL;
644✔
2020
    }
2021
    if (pParam->pTagFields) {
644✔
2022
      taosArrayDestroy(pParam->pTagFields);
443✔
2023
      pParam->pTagFields = NULL;
443✔
2024
    }
2025
    taosMemFree(pParam);
644✔
2026
  }
2027
}
644✔
2028

2029
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
644✔
2030
  int32_t code = 0, lino = 0;
644✔
2031
  if (ppDst == NULL || pSrc == NULL) {
644!
2032
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2033
  }
2034
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
644!
2035
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
644!
2036

2037
  (*ppDst)->suid = pSrc->suid;
644✔
2038
  (*ppDst)->sver = pSrc->sver;
644✔
2039
  (*ppDst)->tbType = pSrc->tbType;
644✔
2040
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
644!
2041
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
644!
2042

2043
  if (pSrc->stbname) {
644!
2044
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
644!
2045
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
644!
2046
  }
2047

2048
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
644!
2049
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
643!
2050

2051
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
643✔
2052

2053
  if (pSrc->pFields && pSrc->pFields->size > 0) {
643!
2054
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
643✔
2055
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
644!
2056
  } else {
2057
    (*ppDst)->pFields = NULL;
×
2058
  }
2059
  
2060
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
644!
2061
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
443✔
2062
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
443!
2063
  } else {
2064
    (*ppDst)->pTagFields = NULL;
201✔
2065
  }
2066

2067
_exit:
644✔
2068

2069
  if (code != 0) {
644!
2070
    if (*ppDst) {
×
2071
      destroyStreamInserterParam(*ppDst);
×
2072
      *ppDst = NULL;
×
2073
    }
2074
    
2075
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2076
  }
2077
  return code;
644✔
2078
}
2079

2080
int32_t dropStreamTable(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq) {
1✔
2081
  return doDropStreamTable(pMsgCb, pOutput, pReq);
1✔
2082
}
2083

2084
int32_t dropStreamTableByTbName(SMsgCb* pMsgCb, void* pOutput, SSTriggerDropRequest* pReq, char* tbName) {
×
2085
  return doDropStreamTableByTbName(pMsgCb, pOutput, pReq, tbName);
×
2086
}
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