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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

53.75
/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 "executorInt.h"
21
#include "libs/new-stream/stream.h"
22
#include "operator.h"
23
#include "osMemPool.h"
24
#include "osMemory.h"
25
#include "planner.h"
26
#include "query.h"
27
#include "querytask.h"
28
#include "storageapi.h"
29
#include "streamexecutorInt.h"
30
#include "tdatablock.h"
31
#include "tref.h"
32
#include "trpc.h"
33
#include "tudf.h"
34
#include "wal.h"
35

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

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

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

56
static void cleanupRefPool() {
2,279✔
57
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
2,279✔
58
  taosCloseRef(ref);
2,279✔
59
}
2,279✔
60

61
static void initRefPool() {
2,279✔
62
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
2,279✔
63
  (void)atexit(cleanupRefPool);
2,279✔
64
}
2,279✔
65

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

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

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

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

119
    return TSDB_CODE_SUCCESS;
×
120
  }
121

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

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

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

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

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

165
  return 0;
46,253✔
166
}
167

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

173
  // set the idstr for tsdbReader
174
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
46,252✔
175
}
176

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

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

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

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

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

200
  return code;
×
201
}
202

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

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

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

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

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

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

239
  // extract the number of output columns
240
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
1,035✔
241
  *numOfCols = 0;
1,035✔
242

243
  SNode* pNode;
244
  FOREACH(pNode, pDescNode->pSlots) {
9,029!
245
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
7,994✔
246
    if (pSlotDesc->output) {
7,994✔
247
      ++(*numOfCols);
7,992✔
248
    }
249
  }
250

251
  return pTaskInfo;
1,035✔
252
}
253

254
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
15,270✔
255
  if (streamInserterParam == NULL) {
15,270✔
256
    return TSDB_CODE_SUCCESS;
14,452✔
257
  }
258

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

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

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

275
  return TSDB_CODE_SUCCESS;
818✔
276
}
277

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

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

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

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

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

333
_error:
622✔
334

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

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

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

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

364
  *pTaskInfo = NULL;
15,270✔
365

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

381
  return code;
15,264✔
382
}
383

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

396
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;
220✔
397

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

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

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

416
    tDecoderClear(&mr.coder);
274✔
417

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

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

444
      if (!qualified) {
110✔
445
        continue;
55✔
446
      }
447
    }
448

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

454
_end:
220✔
455

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

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

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

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

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

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

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

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

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

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

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

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

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

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

564
  return code;
223✔
565
}
566

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

572
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
2,751,726!
573
    return TSDB_CODE_INVALID_PARA;
×
574
  }
575
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
2,751,801✔
576

577
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
2,751,801✔
578
    return TSDB_CODE_SUCCESS;
1,591,651✔
579
  }
580

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

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

601
  *tbGet = true;
1,160,268✔
602

603
  return TSDB_CODE_SUCCESS;
1,160,268✔
604
}
605

606
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
1,591,639✔
607

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

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

620
int32_t qExecutorInit(void) {
14,823✔
621
  (void)taosThreadOnce(&initPoolOnce, initRefPool);
14,823✔
622
  return TSDB_CODE_SUCCESS;
14,830✔
623
}
624

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

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

633
  readHandle->uid = 0;
1,599,417✔
634
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
1,599,417✔
635
  if (code != TSDB_CODE_SUCCESS || NULL == *pTask) {
1,598,811!
636
    qError("failed to createExecTaskInfo, code:%s", tstrerror(code));
×
637
    goto _error;
37✔
638
  }
639

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

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

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

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

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

679
_error:
118,753✔
680
  // if failed to add ref for all tables in this query, abort current query
681
  return code;
1,599,513✔
682
}
683

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

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

696
  if (pLocal) {
1,693,636✔
697
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
1,679,152✔
698
  }
699

700
  taosArrayClear(pResList);
1,693,636✔
701

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

709
  if (pTaskInfo->cost.start == 0) {
1,693,770✔
710
    pTaskInfo->cost.start = taosGetTimestampUs();
1,581,529✔
711
  }
712

713
  if (isTaskKilled(pTaskInfo)) {
1,693,814✔
714
    atomic_store_64(&pTaskInfo->owner, 0);
10✔
715
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
10!
716
    return pTaskInfo->code;
10✔
717
  }
718

719
  // error occurs, record the error code and return to client
720
  int32_t ret = setjmp(pTaskInfo->env);
1,693,536✔
721
  if (ret != TSDB_CODE_SUCCESS) {
1,694,579✔
722
    pTaskInfo->code = ret;
1,004✔
723
    (void)cleanUpUdfs();
1,004✔
724

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

728
    return pTaskInfo->code;
1,004✔
729
  }
730

731
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
1,693,575✔
732

733
  int32_t      current = 0;
1,693,577✔
734
  SSDataBlock* pRes = NULL;
1,693,577✔
735
  int64_t      st = taosGetTimestampUs();
1,693,718✔
736

737
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
1,693,718!
738
    pTaskInfo->paramSet = true;
1,332✔
739
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
1,332✔
740
  } else {
741
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
1,692,386✔
742
  }
743

744
  QUERY_CHECK_CODE(code, lino, _end);
1,692,606!
745
  code = blockDataCheck(pRes);
1,692,606✔
746
  QUERY_CHECK_CODE(code, lino, _end);
1,692,655!
747

748
  if (pRes == NULL) {
1,692,655✔
749
    st = taosGetTimestampUs();
300,137✔
750
  }
751

752
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
1,692,666✔
753
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
1,692,666✔
754
    rowsThreshold = 4096;
1,683,011✔
755
  }
756

757
  int32_t blockIndex = 0;
1,692,666✔
758
  while (pRes != NULL) {
4,738,997✔
759
    SSDataBlock* p = NULL;
3,158,651✔
760
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
3,158,651✔
761
      SSDataBlock* p1 = NULL;
2,651,850✔
762
      code = createOneDataBlock(pRes, true, &p1);
2,651,850✔
763
      QUERY_CHECK_CODE(code, lino, _end);
2,651,996!
764

765
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
2,651,996✔
766
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,651,940!
767
      p = p1;
2,651,940✔
768
    } else {
769
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
507,025✔
770
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
506,893!
771

772
      p = *(SSDataBlock**)tmp;
506,893✔
773
      code = copyDataBlock(p, pRes);
506,893✔
774
      QUERY_CHECK_CODE(code, lino, _end);
508,208!
775
    }
776

777
    blockIndex += 1;
3,160,148✔
778

779
    current += p->info.rows;
3,160,148✔
780
    QUERY_CHECK_CONDITION((p->info.rows > 0 || p->info.type == STREAM_CHECKPOINT), code, lino, _end,
3,160,148!
781
                          TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
782
    void* tmp = taosArrayPush(pResList, &p);
3,159,773✔
783
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,159,773!
784

785
    if (current >= rowsThreshold || processOneBlock) {
3,159,773✔
786
      break;
787
    }
788

789
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
3,047,459✔
790
    QUERY_CHECK_CODE(code, lino, _end);
3,047,158!
791
    code = blockDataCheck(pRes);
3,047,158✔
792
    QUERY_CHECK_CODE(code, lino, _end);
3,046,331!
793
  }
794

795
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
1,692,660✔
796
    pTaskInfo->pSubplan->rowsThreshold -= current;
9,675✔
797
  }
798

799
  *hasMore = (pRes != NULL);
1,692,660✔
800
  uint64_t el = (taosGetTimestampUs() - st);
1,692,677✔
801

802
  pTaskInfo->cost.elapsedTime += el;
1,692,677✔
803
  if (NULL == pRes) {
1,692,677✔
804
    *useconds = pTaskInfo->cost.elapsedTime;
1,580,254✔
805
  }
806

807
_end:
112,423✔
808
  (void)cleanUpUdfs();
1,692,677✔
809

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

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

820
  return pTaskInfo->code;
1,692,774✔
821
}
822

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

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

837
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
280,502✔
838
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
280,502✔
839
  int64_t        threadId = taosGetSelfPthreadId();
280,502✔
840
  int64_t        curOwner = 0;
280,502✔
841

842
  *pRes = NULL;
280,502✔
843

844
  // todo extract method
845
  taosRLockLatch(&pTaskInfo->lock);
280,502✔
846
  bool isKilled = isTaskKilled(pTaskInfo);
280,506✔
847
  if (isKilled) {
280,499!
848
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
849

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

854
  if (pTaskInfo->owner != 0) {
280,499!
855
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
856
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
857

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

862
  pTaskInfo->owner = threadId;
280,499✔
863
  taosRUnLockLatch(&pTaskInfo->lock);
280,499✔
864

865
  if (pTaskInfo->cost.start == 0) {
280,505✔
866
    pTaskInfo->cost.start = taosGetTimestampUs();
733✔
867
  }
868

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

879
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
280,503✔
880

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

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

900
  uint64_t el = (taosGetTimestampUs() - st);
280,466✔
901

902
  pTaskInfo->cost.elapsedTime += el;
280,466✔
903
  if (NULL == *pRes) {
280,466✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
30,412✔
905
  }
906

907
  (void)cleanUpUdfs();
280,466✔
908

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

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

915
  atomic_store_64(&pTaskInfo->owner, 0);
280,505✔
916
  return pTaskInfo->code;
280,507✔
917
}
918

919
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
577,369✔
920
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
577,369✔
921
  void* tmp = taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
577,373✔
922
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
577,374✔
923

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

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

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

941
  return 0;
×
942
}
943

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

953
void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
997✔
954
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
997✔
955

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

978
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
997✔
979
}
997✔
980

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

987
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
997!
988

989
  setTaskKilled(pTaskInfo, rspCode);
997✔
990
  qStopTaskOperators(pTaskInfo);
997✔
991

992
  return TSDB_CODE_SUCCESS;
997✔
993
}
994

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

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

1008
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1009

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

1016
        taosMsleep(200);
×
1017

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

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

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

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

1048
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
1,615,332✔
1049
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1,615,332✔
1050
  int64_t        idleTime = pSummary->start - pSummary->created;
1,615,332✔
1051

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

1067
void qDestroyTask(qTaskInfo_t qTaskHandle) {
1,618,783✔
1068
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
1,618,783✔
1069
  if (pTaskInfo == NULL) {
1,618,783✔
1070
    return;
3,463✔
1071
  }
1072

1073
  if (pTaskInfo->pRoot != NULL) {
1,615,320!
1074
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
1,615,334✔
1075
  } else {
1076
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1077
  }
1078

1079
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
1,615,321✔
1080
  doDestroyTask(pTaskInfo);
1,615,328✔
1081
}
1082

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

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

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

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

1116
void* qExtractReaderFromTmqScanner(void* scanner) {
1,035✔
1117
  SStreamScanInfo* pInfo = scanner;
1,035✔
1118
  return (void*)pInfo->tqReader;
1,035✔
1119
}
1120

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

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

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

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

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

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

1165
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
15,825✔
1166
    SColumnInfo* pColInfo = &pCond->colList[i];
13,312✔
1167
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
13,312✔
1168
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
13,312✔
1169
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
13,312✔
1170
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
13,312✔
1171

1172
    pCond->pSlotList[i] = i;
13,312✔
1173
  }
1174

1175
  return TSDB_CODE_SUCCESS;
2,513✔
1176
}
1177

1178
void qStreamSetOpen(qTaskInfo_t tinfo) {
275,423✔
1179
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
275,423✔
1180
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
275,423✔
1181
  pOperator->status = OP_NOT_OPENED;
275,423✔
1182
}
275,423✔
1183

1184
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
30,298✔
1185
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
30,298✔
1186
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
30,298✔
1187
}
30,298✔
1188

1189
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
33,666✔
1190
  int32_t        code = TSDB_CODE_SUCCESS;
33,666✔
1191
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
33,666✔
1192
  SStorageAPI*   pAPI = &pTaskInfo->storageAPI;
33,666✔
1193

1194
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
33,666✔
1195
  const char*    id = GET_TASKID(pTaskInfo);
33,666✔
1196

1197
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
33,666✔
1198
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
29,414✔
1199
    if (pOperator == NULL || code != 0) {
29,414!
1200
      return code;
×
1201
    }
1202

1203
    SStreamScanInfo* pInfo = pOperator->info;
29,414✔
1204
    SStoreTqReader*  pReaderAPI = &pTaskInfo->storageAPI.tqReaderFn;
29,414✔
1205
    SWalReader*      pWalReader = pReaderAPI->tqReaderGetWalReader(pInfo->tqReader);
29,414✔
1206
    walReaderVerifyOffset(pWalReader, pOffset);
29,413✔
1207
  }
1208
  // if pOffset equal to current offset, means continue consume
1209
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
33,665✔
1210
    return 0;
29,171✔
1211
  }
1212

1213
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
4,494✔
1214
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
1,934✔
1215
    if (pOperator == NULL || code != 0) {
1,934!
1216
      return code;
×
1217
    }
1218

1219
    SStreamScanInfo* pInfo = pOperator->info;
1,934✔
1220
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
1,934✔
1221
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1,934✔
1222
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1,934✔
1223

1224
    if (pOffset->type == TMQ_OFFSET__LOG) {
1,934✔
1225
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
1,120✔
1226
      pScanBaseInfo->dataReader = NULL;
1,120✔
1227

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

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

1253
      if (uid == 0) {
814✔
1254
        if (numOfTables != 0) {
776✔
1255
          STableKeyInfo* tmp = tableListGetInfo(pTableListInfo, 0);
122✔
1256
          if (!tmp) {
122!
1257
            qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1258
            taosRUnLockLatch(&pTaskInfo->lock);
×
1259
            return terrno;
×
1260
          }
1261
          if (tmp) uid = tmp->uid;
122!
1262
          ts = INT64_MIN;
122✔
1263
          pScanInfo->currentTable = 0;
122✔
1264
        } else {
1265
          taosRUnLockLatch(&pTaskInfo->lock);
654✔
1266
          qError("no table in table list, %s", id);
654!
1267
          return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED;
654✔
1268
        }
1269
      }
1270
      pTaskInfo->storageAPI.tqReaderFn.tqSetTablePrimaryKey(pInfo->tqReader, uid);
160✔
1271

1272
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
160!
1273
             pInfo->pTableScanOp->resultInfo.totalRows);
1274
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
160✔
1275

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

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

1290
      STableKeyInfo keyInfo = {.uid = uid};
160✔
1291
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
160✔
1292

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

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

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

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

1328
      // restore the key value
1329
      pScanBaseInfo->cond.twindows.skey = oldSkey;
160✔
1330
    } else {
1331
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
×
1332
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1333
    }
1334

1335
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1336
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
2,560✔
1337
      SStreamRawScanInfo* pInfo = pOperator->info;
2,516✔
1338
      SSnapContext*       sContext = pInfo->sContext;
2,516✔
1339
      SOperatorInfo*      p = NULL;
2,516✔
1340

1341
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
2,516✔
1342
      if (code != 0) {
2,515!
1343
        return code;
×
1344
      }
1345

1346
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
2,515✔
1347

1348
      if (pAPI->snapshotFn.setForSnapShot(sContext, pOffset->uid) != 0) {
2,515!
1349
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
×
1350
        return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1351
      }
1352

1353
      SMetaTableInfo mtInfo = {0};
2,515✔
1354
      code = pTaskInfo->storageAPI.snapshotFn.getMetaTableInfoFromSnapshot(sContext, &mtInfo);
2,515✔
1355
      if (code != 0) {
2,516!
1356
        destroyMetaTableInfo(&mtInfo);
1357
        return code;
×
1358
      }
1359
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
2,516✔
1360
      pInfo->dataReader = NULL;
2,516✔
1361

1362
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,516✔
1363
      tableListClear(pTableListInfo);
2,516✔
1364

1365
      if (mtInfo.uid == 0) {
2,516✔
1366
        destroyMetaTableInfo(&mtInfo);
1367
        goto end;  // no data
3✔
1368
      }
1369

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

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

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

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

1412
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
2,513✔
1413
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
2,513✔
1414
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1415
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
2,513✔
1416
      pTaskInfo->streamInfo.schema = mtInfo.schema;
2,513✔
1417

1418
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
2,513!
1419
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
44✔
1420
      SStreamRawScanInfo* pInfo = pOperator->info;
16✔
1421
      SSnapContext*       sContext = pInfo->sContext;
16✔
1422
      code = pTaskInfo->storageAPI.snapshotFn.setForSnapShot(sContext, pOffset->uid);
16✔
1423
      if (code != 0) {
16!
1424
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
×
1425
        return code;
×
1426
      }
1427
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
16!
1428
             id);
1429
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
28!
1430
      SStreamRawScanInfo* pInfo = pOperator->info;
29✔
1431
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pInfo->dataReader);
29✔
1432
      pInfo->dataReader = NULL;
29✔
1433
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
29!
1434
    }
1435
  }
1436

1437
end:
×
1438
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
3,804✔
1439
  return 0;
3,805✔
1440
}
1441

1442
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
926,382✔
1443
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
926,382✔
1444
  if (pMsg->info.ahandle == NULL) {
926,382!
1445
    qError("pMsg->info.ahandle is NULL");
×
1446
    return;
×
1447
  }
1448

1449
  qDebug("rsp msg got, code:%x, len:%d, 0x%" PRIx64 ":0x%" PRIx64, 
926,382✔
1450
      pMsg->code, pMsg->contLen, TRACE_GET_ROOTID(&pMsg->info.traceId), TRACE_GET_MSGID(&pMsg->info.traceId));
1451

1452
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
926,385✔
1453

1454
  if (pMsg->contLen > 0) {
926,385✔
1455
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
926,356!
1456
    if (buf.pData == NULL) {
926,384!
1457
      pMsg->code = terrno;
×
1458
    } else {
1459
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
926,384✔
1460
    }
1461
  }
1462

1463
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
926,413✔
1464
  rpcFreeCont(pMsg->pCont);
926,493✔
1465
  destroySendMsgInfo(pSendInfo);
926,461✔
1466
}
1467

1468
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1469
  int32_t        code = TSDB_CODE_SUCCESS;
×
1470
  int32_t        lino = 0;
×
1471
  SExecTaskInfo* pTaskInfo = tinfo;
×
1472
  SArray*        plist = NULL;
×
1473

1474
  code = getTableListInfo(pTaskInfo, &plist);
×
1475
  if (code || plist == NULL) {
×
1476
    return NULL;
×
1477
  }
1478

1479
  // only extract table in the first elements
1480
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1481

1482
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1483
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1484

1485
  int32_t numOfTables = 0;
×
1486
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1487
  QUERY_CHECK_CODE(code, lino, _end);
×
1488

1489
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1490
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1491
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1492
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1493
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1494
  }
1495

1496
  taosArrayDestroy(plist);
×
1497

1498
_end:
×
1499
  if (code != TSDB_CODE_SUCCESS) {
×
1500
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1501
    T_LONG_JMP(pTaskInfo->env, code);
×
1502
  }
1503
  return pUidList;
×
1504
}
1505

1506
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
14,016✔
1507
  int32_t        code = TSDB_CODE_SUCCESS;
14,016✔
1508
  int32_t        lino = 0;
14,016✔
1509
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
14,016✔
1510

1511
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14,016!
1512
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1513
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1514

1515
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1516
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1517
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
14,016✔
1518
    STableScanInfo* pScanInfo = pOperator->info;
7,008✔
1519

1520
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
7,008✔
1521
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
7,008!
1522
  } else {
1523
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
7,008!
1524
      code = extractTableList(pList, pOperator->pDownstream[0]);
7,008✔
1525
    }
1526
  }
1527

1528
_end:
×
1529
  if (code != TSDB_CODE_SUCCESS) {
14,016!
1530
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1531
  }
1532
  return code;
14,016✔
1533
}
1534

1535
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
7,008✔
1536
  if (pList == NULL) {
7,008!
1537
    return TSDB_CODE_INVALID_PARA;
×
1538
  }
1539

1540
  *pList = NULL;
7,008✔
1541
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
7,008✔
1542
  if (pArray == NULL) {
7,008!
1543
    return terrno;
×
1544
  }
1545

1546
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
7,008✔
1547
  if (code == 0) {
7,008!
1548
    *pList = pArray;
7,008✔
1549
  } else {
1550
    taosArrayDestroy(pArray);
×
1551
  }
1552
  return code;
7,008✔
1553
}
1554

1555
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1556
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1557
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1558
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1559
  }
1560
  return 0;
×
1561
}
1562

1563
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1564
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1565
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1566
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1567
  }
1568
  return 0;
×
1569
}
1570

1571
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1572
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1573

1574
  int32_t code = pTaskInfo->code;
×
1575
  pTaskInfo->code = 0;
×
1576
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1577
}
×
1578

1579
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1580
  int32_t code = 0;
×
1581
  return code;
×
1582
}
1583

1584
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1585
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1586
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1587
  return code;
×
1588
}
1589

1590
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
51,802✔
1591
  int32_t code = 0;
51,802✔
1592

1593
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
51,802✔
1594
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
51,802✔
1595

1596
  if (pOper->fpSet.resetStateFn) {
51,802✔
1597
    code = pOper->fpSet.resetStateFn(pOper);
51,801✔
1598
  }
1599
  pOper->status = OP_NOT_OPENED;
51,802✔
1600
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
81,651!
1601
    code = clearStatesForOperator(pOper->pDownstream[i]);
29,847✔
1602
  }
1603
  return code;
51,804✔
1604
}
1605

1606
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
21,955✔
1607
  int32_t        code = 0;
21,955✔
1608
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
21,955✔
1609
  SOperatorInfo* pOper = pTaskInfo->pRoot;
21,955✔
1610
  code = clearStatesForOperator(pOper);
21,955✔
1611
  return code;
21,955✔
1612
}
1613

1614
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
32,738✔
1615
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
32,738✔
1616
  int64_t        threadId = taosGetSelfPthreadId();
32,738✔
1617
  int64_t        curOwner = 0;
32,738✔
1618

1619
  *ppRes = NULL;
32,738✔
1620

1621
  // todo extract method
1622
  taosRLockLatch(&pTaskInfo->lock);
32,738✔
1623
  bool isKilled = isTaskKilled(pTaskInfo);
32,738✔
1624
  if (isKilled) {
32,738!
1625
    // clearStreamBlock(pTaskInfo->pRoot);
1626
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1627

1628
    taosRUnLockLatch(&pTaskInfo->lock);
×
1629
    return pTaskInfo->code;
×
1630
  }
1631

1632
  if (pTaskInfo->owner != 0) {
32,738!
1633
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1634
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1635

1636
    taosRUnLockLatch(&pTaskInfo->lock);
×
1637
    return pTaskInfo->code;
×
1638
  }
1639

1640
  pTaskInfo->owner = threadId;
32,738✔
1641
  taosRUnLockLatch(&pTaskInfo->lock);
32,738✔
1642

1643
  if (pTaskInfo->cost.start == 0) {
32,739✔
1644
    pTaskInfo->cost.start = taosGetTimestampUs();
818✔
1645
  }
1646

1647
  // error occurs, record the error code and return to client
1648
  int32_t ret = setjmp(pTaskInfo->env);
32,739✔
1649
  if (ret != TSDB_CODE_SUCCESS) {
32,755✔
1650
    pTaskInfo->code = ret;
16✔
1651
    (void)cleanUpUdfs();
16✔
1652
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
16!
1653
    atomic_store_64(&pTaskInfo->owner, 0);
16✔
1654
    return pTaskInfo->code;
16✔
1655
  }
1656

1657
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
32,739✔
1658

1659
  int64_t st = taosGetTimestampUs();
32,739✔
1660

1661
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
32,739✔
1662
  if (code) {
32,723!
1663
    pTaskInfo->code = code;
×
1664
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1665
  } else {
1666
    *finished = *ppRes == NULL;
32,723✔
1667
    code = blockDataCheck(*ppRes);
32,723✔
1668
  }
1669
  if (code) {
32,723!
1670
    pTaskInfo->code = code;
×
1671
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1672
  }
1673

1674
  uint64_t el = (taosGetTimestampUs() - st);
32,723✔
1675

1676
  pTaskInfo->cost.elapsedTime += el;
32,723✔
1677
  if (NULL == *ppRes) {
32,723✔
1678
    *useconds = pTaskInfo->cost.elapsedTime;
21,533✔
1679
  }
1680

1681
  (void)cleanUpUdfs();
32,723✔
1682

1683
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
32,723✔
1684
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
32,723✔
1685

1686
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
32,723✔
1687
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1688

1689
  atomic_store_64(&pTaskInfo->owner, 0);
32,723✔
1690
  return pTaskInfo->code;
32,722✔
1691
}
1692

1693
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1694
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1695
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1696
// }
1697

1698
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
36,140✔
1699
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1700
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1701
  STableListInfo* pList = tableListCreate();
36,140✔
1702
  if (pList == NULL) {
36,097!
1703
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1704
    return terrno;
×
1705
  }
1706

1707
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
36,097✔
1708
  SReadHandle    pHandle = {.vnode = pVnode};
36,097✔
1709
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
36,097✔
1710

1711
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond, &pTaskInfo, groupIdMap);
36,097✔
1712
  if (code != 0) {
36,136✔
1713
    tableListDestroy(pList);
2✔
1714
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1715
    return code;
×
1716
  }
1717
  *pTableListInfo = pList;
36,134✔
1718
  return 0;
36,134✔
1719
}
1720

1721
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
30,252✔
1722
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
30,252!
1723
    return TSDB_CODE_INVALID_PARA;
×
1724
  }
1725
  if (taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList) == 0) {
30,252✔
1726
    *size = 0;
22,822✔
1727
    *pKeyInfo = NULL;
22,822✔
1728
    return 0;
22,822✔
1729
  }
1730
  if (currentGroupId == -1) {
7,430✔
1731
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
573✔
1732
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
573✔
1733
    return 0;
573✔
1734
  }
1735
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
6,857✔
1736
}
1737

1738
int32_t  qStreamSetTableList(void** pTableListInfo, STableKeyInfo* data){
1,541✔
1739
  if (*pTableListInfo == NULL) {
1,541✔
1740
    *pTableListInfo = tableListCreate();
475✔
1741
    if (*pTableListInfo == NULL) {
475!
1742
      return terrno;
×
1743
    }
1744
  }
1745
  return taosArrayPush(((STableListInfo*)(*pTableListInfo))->pTableList, data) != NULL ? 0 : terrno;
3,082!
1746
}
1747

1748
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
27,666✔
1749
  if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
27,666✔
1750
    return 0;
22,675✔
1751
  }
1752
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
5,538✔
1753
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
5,463✔
1754

1755
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
5,463✔
1756
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
5,463!
1757
      return i;
4,916✔
1758
    }
1759
  }
1760
  return -1;
75✔
1761
}
1762

1763
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
38,369✔
1764

1765
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
7,120✔
1766

1767
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
1,762✔
1768
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
196✔
1769

1770
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo) { return doFilter(pBlock, pFilterInfo, NULL); }
8,514✔
1771

1772
bool qStreamUidInTableList(void* pTableListInfo, uint64_t uid) {
31,416✔
1773
  return tableListGetTableGroupId(pTableListInfo, uid) != -1;
31,416✔
1774
}
1775

1776
void streamDestroyExecTask(qTaskInfo_t tInfo) {
2,673✔
1777
  qInfo("streamDestroyExecTask called, task:%p", tInfo);
2,673!
1778
  qDestroyTask(tInfo);
2,673✔
1779
}
2,673✔
1780

1781
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
1,881✔
1782
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
1,881✔
1783
}
1784

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

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

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

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

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

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

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

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

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

1983
    memcpy(tbname, pVal, len);
646✔
1984
    tbname[len] = '\0';  // ensure null terminated
646✔
1985
    if (NULL != strchr(tbname, '.')) {
646!
1986
      code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME;
×
1987
      qError("tbname generated with invalid characters, '.' is not allowed");
×
1988
    }
1989
  }
1990
  // TODO free dst
1991
  sclFreeParam(&dst);
646✔
1992
  return code;
646✔
1993
}
1994

1995
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
818✔
1996
  if (pParam) {
818!
1997
    if (pParam->tbname) {
818!
1998
      taosMemFree(pParam->tbname);
818✔
1999
      pParam->tbname = NULL;
818✔
2000
    }
2001
    if (pParam->stbname) {
818!
2002
      taosMemFree(pParam->stbname);
818✔
2003
      pParam->stbname = NULL;
818✔
2004
    }
2005
    if (pParam->dbFName) {
818!
2006
      taosMemFree(pParam->dbFName);
818✔
2007
      pParam->dbFName = NULL;
818✔
2008
    }
2009
    if (pParam->pFields) {
818!
2010
      taosArrayDestroy(pParam->pFields);
818✔
2011
      pParam->pFields = NULL;
818✔
2012
    }
2013
    if (pParam->pTagFields) {
818✔
2014
      taosArrayDestroy(pParam->pTagFields);
577✔
2015
      pParam->pTagFields = NULL;
577✔
2016
    }
2017
    taosMemFree(pParam);
818✔
2018
  }
2019
}
818✔
2020

2021
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
818✔
2022
  int32_t code = 0, lino = 0;
818✔
2023
  if (ppDst == NULL || pSrc == NULL) {
818!
2024
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
2025
  }
2026
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
818!
2027
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
818!
2028

2029
  (*ppDst)->suid = pSrc->suid;
818✔
2030
  (*ppDst)->sver = pSrc->sver;
818✔
2031
  (*ppDst)->tbType = pSrc->tbType;
818✔
2032
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
818!
2033
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
818!
2034

2035
  if (pSrc->stbname) {
818!
2036
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
818!
2037
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
818!
2038
  }
2039

2040
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
818!
2041
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
818!
2042

2043
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
818✔
2044

2045
  if (pSrc->pFields && pSrc->pFields->size > 0) {
818!
2046
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
818✔
2047
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
818!
2048
  } else {
2049
    (*ppDst)->pFields = NULL;
×
2050
  }
2051
  
2052
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
818!
2053
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
577✔
2054
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
577!
2055
  } else {
2056
    (*ppDst)->pTagFields = NULL;
241✔
2057
  }
2058

2059
_exit:
818✔
2060

2061
  if (code != 0) {
818!
2062
    if (*ppDst) {
×
2063
      destroyStreamInserterParam(*ppDst);
×
2064
      *ppDst = NULL;
×
2065
    }
2066
    
2067
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2068
  }
2069
  return code;
818✔
2070
}
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

© 2025 Coveralls, Inc