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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

17.01
/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) {
15✔
41
  gExecInfo.dnode = pDnode;
15✔
42
  gExecInfo.getMnode = getMnode;
15✔
43
  gExecInfo.getDnodeId = getDnodeId;
15✔
44
  return;
15✔
45
}
46

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

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

61
static void initRefPool() {
15✔
62
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
15✔
63
  (void)atexit(cleanupRefPool);
15✔
64
}
15✔
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) {
30✔
148
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
30✔
149
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
30✔
150
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
22✔
151
    if (pStreamScanInfo->pTableScanOp != NULL) {
22!
152
      STableScanInfo* pScanInfo = pStreamScanInfo->pTableScanOp->info;
22✔
153
      if (pScanInfo->base.dataReader != NULL) {
22!
154
        int32_t code = pAPI->tsdReader.tsdSetReaderTaskId(pScanInfo->base.dataReader, pTaskInfo->id.str);
×
155
        if (code) {
×
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);
8!
163
  }
164

165
  return 0;
22✔
166
}
167

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

173
  // set the idstr for tsdbReader
174
  return doSetTaskId(pTaskInfo->pRoot, &pTaskInfo->storageAPI);
22✔
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,
9✔
204
                                     uint64_t id) {
205
  if (msg == NULL) {  // create raw scan
9!
206
    SExecTaskInfo* pTaskInfo = NULL;
×
207

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

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

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

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

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

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

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

251
  return pTaskInfo;
9✔
252
}
253

254
static int32_t checkInsertParam(SStreamInserterParam* streamInserterParam) {
×
255
  if (streamInserterParam == NULL) {
×
256
    return TSDB_CODE_SUCCESS;
×
257
  }
258

259
  if (streamInserterParam->tbType == TSDB_SUPER_TABLE && streamInserterParam->suid <= 0) {
×
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) {
×
265
    stError("insertParam: invalid db/table name");
×
266
    return TSDB_CODE_INVALID_PARA;
×
267
  }
268

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

275
  return TSDB_CODE_SUCCESS;
×
276
}
277

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

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

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

313
    pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
×
314
    if (NULL == pInserterParam) {
×
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);
×
320
    TSDB_CHECK_CODE(code, lino, _error);
×
321
    
322
    pInserterParam->readHandle = taosMemCalloc(1, sizeof(SReadHandle));
×
323
    pInserterParam->readHandle->pMsgCb = readHandle->pMsgCb;
×
324

325
    code = createStreamDataInserter(pSinkManager, handle, pInserterParam);
×
326
    if (code) {
×
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,
×
331
         tstrerror(code));
332

333
_error:
×
334

335
  if (code != TSDB_CODE_SUCCESS) {
×
336
    qError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
337
    if (pInserterParam != NULL) {
×
338
      taosMemoryFree(pInserterParam);
×
339
    }
340
  }
341
  return code;
×
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,
×
359
                                  SStreamInserterParam* pInserterParams, int32_t vgId, int32_t taskId) {
360
  if (msg == NULL) {
×
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363

364
  *pTaskInfo = NULL;
×
365

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

381
  return code;
×
382
}
383

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

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

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

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

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

416
    tDecoderClear(&mr.coder);
×
417

418
    if (mr.me.type == TSDB_SUPER_TABLE) {
×
419
      continue;
×
420
    } else {
421
      if (type == TSDB_SUPER_TABLE) {
×
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) {
×
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) {
×
436
      bool          qualified = false;
×
437
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
×
438
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.vnode, &qualified, pAPI);
×
439
      if (code != TSDB_CODE_SUCCESS) {
×
440
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
×
441
        continue;
×
442
      }
443

444
      if (!qualified) {
×
445
        continue;
×
446
      }
447
    }
448

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

454
_end:
×
455

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

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

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

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

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

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

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

502
    bool   assignUid = false;
×
503
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
×
504
    char*  keyBuf = NULL;
×
505
    if (bufLen > 0) {
×
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;
×
515
    taosWLockLatch(&pTaskInfo->lock);
×
516

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

527
      if (bufLen > 0) {
×
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);
×
543
      if (code != TSDB_CODE_SUCCESS) {
×
544
        taosMemoryFree(keyBuf);
×
545
        taosArrayDestroy(qa);
×
546
        taosWUnLockLatch(&pTaskInfo->lock);
×
547
        return code;
×
548
      }
549
    }
550

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

556
    taosArrayDestroy(qa);
×
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);
×
559
    taosWLockLatch(&pTaskInfo->lock);
×
560
    pTaskInfo->storageAPI.tqReaderFn.tqReaderRemoveTables(pScanInfo->tqReader, tableIdList);
×
561
    taosWUnLockLatch(&pTaskInfo->lock);
×
562
  }
563

564
  return code;
×
565
}
566

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

572
  if (tinfo == NULL || dbName == NULL || tableName == NULL) {
32,513!
573
    return TSDB_CODE_INVALID_PARA;
×
574
  }
575
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
32,514✔
576

577
  if (taosArrayGetSize(pTaskInfo->schemaInfos) <= idx) {
32,514✔
578
    return TSDB_CODE_SUCCESS;
17,563✔
579
  }
580

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

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

601
  *tbGet = true;
14,951✔
602

603
  return TSDB_CODE_SUCCESS;
14,951✔
604
}
605

606
bool qIsDynamicExecTask(qTaskInfo_t tinfo) { return ((SExecTaskInfo*)tinfo)->dynamicTask; }
17,564✔
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) {
342✔
616
  TSWAP(pParam, ((SExecTaskInfo*)tinfo)->pOpParam);
342✔
617
  ((SExecTaskInfo*)tinfo)->paramSet = false;
342✔
618
}
342✔
619

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

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

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

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

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

649
    void* pSinkParam = NULL;
17,559✔
650
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
17,559✔
651
    if (code != TSDB_CODE_SUCCESS) {
17,556!
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;
17,556✔
658
    if (readHandle->localExec) {
17,556!
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),
1!
662
               (*pTask)->id.str);
663
        taosMemoryFree(pSinkManager);
1!
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,
17,555✔
670
                              (*pTask)->id.str, pSubplan->processOneBlock);
17,555!
671
    if (code) {
17,564!
672
      qError("s-task:%s failed to create data sinker, code:%s", (*pTask)->id.str, tstrerror(code));
×
673
    }
674
  }
675

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

679
_error:
×
680
  // if failed to add ref for all tables in this query, abort current query
681
  return code;
17,573✔
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,
17,394✔
690
                     bool processOneBlock) {
691
  int32_t        code = TSDB_CODE_SUCCESS;
17,394✔
692
  int32_t        lino = 0;
17,394✔
693
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
17,394✔
694
  int64_t        threadId = taosGetSelfPthreadId();
17,394✔
695

696
  if (pLocal) {
17,394!
697
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
17,394✔
698
  }
699

700
  taosArrayClear(pResList);
17,394✔
701

702
  int64_t curOwner = 0;
17,394✔
703
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
17,394!
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) {
17,394✔
710
    pTaskInfo->cost.start = taosGetTimestampUs();
17,388✔
711
  }
712

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

719
  // error occurs, record the error code and return to client
720
  int32_t ret = setjmp(pTaskInfo->env);
17,394✔
721
  if (ret != TSDB_CODE_SUCCESS) {
17,430✔
722
    pTaskInfo->code = ret;
36✔
723
    (void)cleanUpUdfs();
36✔
724

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

728
    return pTaskInfo->code;
36✔
729
  }
730

731
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
17,394!
732

733
  int32_t      current = 0;
17,394✔
734
  SSDataBlock* pRes = NULL;
17,394✔
735
  int64_t      st = taosGetTimestampUs();
17,394✔
736

737
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
17,394!
738
    pTaskInfo->paramSet = true;
342✔
739
    code = pTaskInfo->pRoot->fpSet.getNextExtFn(pTaskInfo->pRoot, pTaskInfo->pOpParam, &pRes);
342✔
740
  } else {
741
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
17,052✔
742
  }
743

744
  QUERY_CHECK_CODE(code, lino, _end);
17,358!
745
  code = blockDataCheck(pRes);
17,358✔
746
  QUERY_CHECK_CODE(code, lino, _end);
17,357!
747

748
  if (pRes == NULL) {
17,357✔
749
    st = taosGetTimestampUs();
4,969✔
750
  }
751

752
  int32_t rowsThreshold = pTaskInfo->pSubplan->rowsThreshold;
17,357✔
753
  if (!pTaskInfo->pSubplan->dynamicRowThreshold || 4096 <= pTaskInfo->pSubplan->rowsThreshold) {
17,357!
754
    rowsThreshold = 4096;
17,357✔
755
  }
756

757
  int32_t blockIndex = 0;
17,357✔
758
  while (pRes != NULL) {
32,653✔
759
    SSDataBlock* p = NULL;
15,300✔
760
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
15,300✔
761
      SSDataBlock* p1 = NULL;
15,296✔
762
      code = createOneDataBlock(pRes, true, &p1);
15,296✔
763
      QUERY_CHECK_CODE(code, lino, _end);
15,295!
764

765
      void* tmp = taosArrayPush(pTaskInfo->pResultBlockList, &p1);
15,295✔
766
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
15,296!
767
      p = p1;
15,296✔
768
    } else {
769
      void* tmp = taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
4✔
770
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
4!
771

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

777
    blockIndex += 1;
15,300✔
778

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

785
    if (current >= rowsThreshold || processOneBlock) {
15,298!
786
      break;
787
    }
788

789
    code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, &pRes);
15,292✔
790
    QUERY_CHECK_CODE(code, lino, _end);
15,297!
791
    code = blockDataCheck(pRes);
15,297✔
792
    QUERY_CHECK_CODE(code, lino, _end);
15,296!
793
  }
794

795
  if (pTaskInfo->pSubplan->dynamicRowThreshold) {
17,359!
796
    pTaskInfo->pSubplan->rowsThreshold -= current;
×
797
  }
798

799
  *hasMore = (pRes != NULL);
17,359✔
800
  uint64_t el = (taosGetTimestampUs() - st);
17,357✔
801

802
  pTaskInfo->cost.elapsedTime += el;
17,357✔
803
  if (NULL == pRes) {
17,357✔
804
    *useconds = pTaskInfo->cost.elapsedTime;
17,351✔
805
  }
806

807
_end:
6✔
808
  (void)cleanUpUdfs();
17,357✔
809

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

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

820
  return pTaskInfo->code;
17,358✔
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) {
382✔
838
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
382✔
839
  int64_t        threadId = taosGetSelfPthreadId();
382✔
840
  int64_t        curOwner = 0;
382✔
841

842
  *pRes = NULL;
382✔
843

844
  // todo extract method
845
  taosRLockLatch(&pTaskInfo->lock);
382✔
846
  bool isKilled = isTaskKilled(pTaskInfo);
382✔
847
  if (isKilled) {
382!
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) {
382!
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;
382✔
863
  taosRUnLockLatch(&pTaskInfo->lock);
382✔
864

865
  if (pTaskInfo->cost.start == 0) {
382✔
866
    pTaskInfo->cost.start = taosGetTimestampUs();
9✔
867
  }
868

869
  // error occurs, record the error code and return to client
870
  int32_t ret = setjmp(pTaskInfo->env);
382✔
871
  if (ret != TSDB_CODE_SUCCESS) {
382!
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));
382!
880

881
  int64_t st = taosGetTimestampUs();
382✔
882
  int32_t code = TSDB_CODE_SUCCESS;
382✔
883
  if (pTaskInfo->pOpParam && !pTaskInfo->paramSet) {
382!
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);
382✔
888
  }
889
  if (code) {
382!
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);
382✔
895
  if (code) {
382!
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);
382✔
901

902
  pTaskInfo->cost.elapsedTime += el;
382✔
903
  if (NULL == *pRes) {
382✔
904
    *useconds = pTaskInfo->cost.elapsedTime;
22✔
905
  }
906

907
  (void)cleanUpUdfs();
382✔
908

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

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

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

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

924
  if (!tmp) {
14,195!
925
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
926
    return terrno;
×
927
  }
928
  return TSDB_CODE_SUCCESS;
14,195✔
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) {
×
954
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
×
955

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

976
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
×
977
}
×
978

979
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
×
980
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
×
981
  if (pTaskInfo == NULL) {
×
982
    return TSDB_CODE_QRY_INVALID_QHANDLE;
×
983
  }
984

985
  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
×
986

987
  setTaskKilled(pTaskInfo, rspCode);
×
988
  qStopTaskOperators(pTaskInfo);
×
989

990
  return TSDB_CODE_SUCCESS;
×
991
}
992

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

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

1006
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
×
1007

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

1014
        taosMsleep(200);
×
1015

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

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

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

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

1046
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
17,573✔
1047
  STaskCostInfo* pSummary = &pTaskInfo->cost;
17,573✔
1048
  int64_t        idleTime = pSummary->start - pSummary->created;
17,573✔
1049

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

1065
void qDestroyTask(qTaskInfo_t qTaskHandle) {
17,573✔
1066
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
17,573✔
1067
  if (pTaskInfo == NULL) {
17,573!
1068
    return;
×
1069
  }
1070

1071
  if (pTaskInfo->pRoot != NULL) {
17,573!
1072
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
17,573!
1073
  } else {
1074
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
×
1075
  }
1076

1077
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
17,573✔
1078
  doDestroyTask(pTaskInfo);
17,573✔
1079
}
1080

1081
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
3,844✔
1082
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
3,844✔
1083
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
3,844✔
1084
}
1085

1086
void qExtractTmqScanner(qTaskInfo_t tinfo, void** scanner) {
9✔
1087
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
9✔
1088
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
9✔
1089

1090
  while (1) {
5✔
1091
    uint16_t type = pOperator->operatorType;
14✔
1092
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
14✔
1093
      *scanner = pOperator->info;
9✔
1094
      break;
9✔
1095
    } else {
1096
      pOperator = pOperator->pDownstream[0];
5✔
1097
    }
1098
  }
1099
}
9✔
1100

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

1114
void* qExtractReaderFromTmqScanner(void* scanner) {
9✔
1115
  SStreamScanInfo* pInfo = scanner;
9✔
1116
  return (void*)pInfo->tqReader;
9✔
1117
}
1118

1119
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
×
1120
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1121
  return pTaskInfo->streamInfo.schema;
×
1122
}
1123

1124
const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
×
1125
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1126
  return pTaskInfo->streamInfo.tbName;
×
1127
}
1128

1129
SMqBatchMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
×
1130
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1131
  return &pTaskInfo->streamInfo.btMetaRsp;
×
1132
}
1133

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

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

1157
  TAOS_SET_OBJ_ALIGNED(&pCond->twindows, TSWINDOW_INITIALIZER);
×
1158
  pCond->suid = pMtInfo->suid;
×
1159
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
×
1160
  pCond->startVersion = -1;
×
1161
  pCond->endVersion = sContext->snapVersion;
×
1162

1163
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
×
1164
    SColumnInfo* pColInfo = &pCond->colList[i];
×
1165
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
×
1166
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
×
1167
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;
×
1168
    pColInfo->pk = pMtInfo->schema->pSchema[i].flags & COL_IS_KEY;
×
1169

1170
    pCond->pSlotList[i] = i;
×
1171
  }
1172

1173
  return TSDB_CODE_SUCCESS;
×
1174
}
1175

1176
void qStreamSetOpen(qTaskInfo_t tinfo) {
382✔
1177
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
382✔
1178
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
382✔
1179
  pOperator->status = OP_NOT_OPENED;
382✔
1180
}
382✔
1181

1182
void qStreamSetSourceExcluded(qTaskInfo_t tinfo, int8_t sourceExcluded) {
22✔
1183
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
22✔
1184
  pTaskInfo->streamInfo.sourceExcluded = sourceExcluded;
22✔
1185
}
22✔
1186

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

1192
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
22✔
1193
  const char*    id = GET_TASKID(pTaskInfo);
22✔
1194

1195
  if (subType == TOPIC_SUB_TYPE__COLUMN && pOffset->type == TMQ_OFFSET__LOG) {
22!
1196
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
22✔
1197
    if (pOperator == NULL || code != 0) {
22!
1198
      return code;
×
1199
    }
1200

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

1211
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
9!
1212
    code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id, &pOperator);
9✔
1213
    if (pOperator == NULL || code != 0) {
9!
1214
      return code;
×
1215
    }
1216

1217
    SStreamScanInfo* pInfo = pOperator->info;
9✔
1218
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
9✔
1219
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
9✔
1220
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
9✔
1221

1222
    if (pOffset->type == TMQ_OFFSET__LOG) {
9!
1223
      pTaskInfo->storageAPI.tsdReader.tsdReaderClose(pScanBaseInfo->dataReader);
9✔
1224
      pScanBaseInfo->dataReader = NULL;
9✔
1225

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

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

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

1270
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
×
1271
             pInfo->pTableScanOp->resultInfo.totalRows);
1272
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
×
1273

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

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

1288
      STableKeyInfo keyInfo = {.uid = uid};
×
1289
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
×
1290

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

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

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

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

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

1333
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1334
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
×
1335
      SStreamRawScanInfo* pInfo = pOperator->info;
×
1336
      SSnapContext*       sContext = pInfo->sContext;
×
1337
      SOperatorInfo*      p = NULL;
×
1338

1339
      code = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id, &p);
×
1340
      if (code != 0) {
×
1341
        return code;
×
1342
      }
1343

1344
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;
×
1345

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

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

1360
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
×
1361
      tableListClear(pTableListInfo);
×
1362

1363
      if (mtInfo.uid == 0) {
×
1364
        destroyMetaTableInfo(&mtInfo);
1365
        goto end;  // no data
×
1366
      }
1367

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

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

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

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

1410
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
×
1411
      tstrncpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName, TSDB_TABLE_NAME_LEN);
×
1412
      //      pTaskInfo->streamInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
1413
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
×
1414
      pTaskInfo->streamInfo.schema = mtInfo.schema;
×
1415

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

1435
end:
×
1436
  tOffsetCopy(&pTaskInfo->streamInfo.currentOffset, pOffset);
9✔
1437
  return 0;
9✔
1438
}
1439

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

1447
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};
15,001✔
1448

1449
  if (pMsg->contLen > 0) {
15,001!
1450
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
15,001!
1451
    if (buf.pData == NULL) {
15,001!
1452
      pMsg->code = terrno;
×
1453
    } else {
1454
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
15,001✔
1455
    }
1456
  }
1457

1458
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
15,001✔
1459
  rpcFreeCont(pMsg->pCont);
15,001✔
1460
  destroySendMsgInfo(pSendInfo);
15,000✔
1461
}
1462

1463
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
×
1464
  int32_t        code = TSDB_CODE_SUCCESS;
×
1465
  int32_t        lino = 0;
×
1466
  SExecTaskInfo* pTaskInfo = tinfo;
×
1467
  SArray*        plist = NULL;
×
1468

1469
  code = getTableListInfo(pTaskInfo, &plist);
×
1470
  if (code || plist == NULL) {
×
1471
    return NULL;
×
1472
  }
1473

1474
  // only extract table in the first elements
1475
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);
×
1476

1477
  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));
×
1478
  QUERY_CHECK_NULL(pUidList, code, lino, _end, terrno);
×
1479

1480
  int32_t numOfTables = 0;
×
1481
  code = tableListGetSize(pTableListInfo, &numOfTables);
×
1482
  QUERY_CHECK_CODE(code, lino, _end);
×
1483

1484
  for (int32_t i = 0; i < numOfTables; ++i) {
×
1485
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
×
1486
    QUERY_CHECK_NULL(pKeyInfo, code, lino, _end, terrno);
×
1487
    void* tmp = taosArrayPush(pUidList, &pKeyInfo->uid);
×
1488
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1489
  }
1490

1491
  taosArrayDestroy(plist);
×
1492

1493
_end:
×
1494
  if (code != TSDB_CODE_SUCCESS) {
×
1495
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1496
    T_LONG_JMP(pTaskInfo->env, code);
×
1497
  }
1498
  return pUidList;
×
1499
}
1500

1501
static int32_t extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
×
1502
  int32_t        code = TSDB_CODE_SUCCESS;
×
1503
  int32_t        lino = 0;
×
1504
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
×
1505

1506
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
×
1507
    SStreamScanInfo* pScanInfo = pOperator->info;
×
1508
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
×
1509

1510
    void* tmp = taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
×
1511
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1512
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
×
1513
    STableScanInfo* pScanInfo = pOperator->info;
×
1514

1515
    void* tmp = taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
×
1516
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1517
  } else {
1518
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
×
1519
      code = extractTableList(pList, pOperator->pDownstream[0]);
×
1520
    }
1521
  }
1522

1523
_end:
×
1524
  if (code != TSDB_CODE_SUCCESS) {
×
1525
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1526
  }
1527
  return code;
×
1528
}
1529

1530
int32_t getTableListInfo(const SExecTaskInfo* pTaskInfo, SArray** pList) {
×
1531
  if (pList == NULL) {
×
1532
    return TSDB_CODE_INVALID_PARA;
×
1533
  }
1534

1535
  *pList = NULL;
×
1536
  SArray* pArray = taosArrayInit(0, POINTER_BYTES);
×
1537
  if (pArray == NULL) {
×
1538
    return terrno;
×
1539
  }
1540

1541
  int32_t code = extractTableList(pArray, pTaskInfo->pRoot);
×
1542
  if (code == 0) {
×
1543
    *pList = pArray;
×
1544
  } else {
1545
    taosArrayDestroy(pArray);
×
1546
  }
1547
  return code;
×
1548
}
1549

1550
int32_t qStreamOperatorReleaseState(qTaskInfo_t tInfo) {
×
1551
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1552
  if (pTaskInfo->pRoot->fpSet.releaseStreamStateFn != NULL) {
×
1553
    pTaskInfo->pRoot->fpSet.releaseStreamStateFn(pTaskInfo->pRoot);
×
1554
  }
1555
  return 0;
×
1556
}
1557

1558
int32_t qStreamOperatorReloadState(qTaskInfo_t tInfo) {
×
1559
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1560
  if (pTaskInfo->pRoot->fpSet.reloadStreamStateFn != NULL) {
×
1561
    pTaskInfo->pRoot->fpSet.reloadStreamStateFn(pTaskInfo->pRoot);
×
1562
  }
1563
  return 0;
×
1564
}
1565

1566
void qResetTaskCode(qTaskInfo_t tinfo) {
×
1567
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
×
1568

1569
  int32_t code = pTaskInfo->code;
×
1570
  pTaskInfo->code = 0;
×
1571
  qDebug("0x%" PRIx64 " reset task code to be success, prev:%s", pTaskInfo->id.taskId, tstrerror(code));
×
1572
}
×
1573

1574
int32_t collectExprsToReplaceForStream(SOperatorInfo* pOper, SArray* pExprs) {
×
1575
  int32_t code = 0;
×
1576
  return code;
×
1577
}
1578

1579
int32_t streamCollectExprsForReplace(qTaskInfo_t tInfo, SArray* pExprs) {
×
1580
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1581
  int32_t        code = collectExprsToReplaceForStream(pTaskInfo->pRoot, pExprs);
×
1582
  return code;
×
1583
}
1584

1585
int32_t clearStatesForOperator(SOperatorInfo* pOper) {
×
1586
  int32_t code = 0;
×
1587

1588
  freeResetOperatorParams(pOper, OP_GET_PARAM, true);
×
1589
  freeResetOperatorParams(pOper, OP_NOTIFY_PARAM, true);
×
1590

1591
  if (pOper->fpSet.resetStateFn) {
×
1592
    code = pOper->fpSet.resetStateFn(pOper);
×
1593
  }
1594
  pOper->status = OP_NOT_OPENED;
×
1595
  for (int32_t i = 0; i < pOper->numOfDownstream && code == 0; ++i) {
×
1596
    code = clearStatesForOperator(pOper->pDownstream[i]);
×
1597
  }
1598
  return code;
×
1599
}
1600

1601
int32_t streamClearStatesForOperators(qTaskInfo_t tInfo) {
×
1602
  int32_t        code = 0;
×
1603
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1604
  SOperatorInfo* pOper = pTaskInfo->pRoot;
×
1605
  code = clearStatesForOperator(pOper);
×
1606
  return code;
×
1607
}
1608

1609
int32_t streamExecuteTask(qTaskInfo_t tInfo, SSDataBlock** ppRes, uint64_t* useconds, bool* finished) {
×
1610
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1611
  int64_t        threadId = taosGetSelfPthreadId();
×
1612
  int64_t        curOwner = 0;
×
1613

1614
  *ppRes = NULL;
×
1615

1616
  // todo extract method
1617
  taosRLockLatch(&pTaskInfo->lock);
×
1618
  bool isKilled = isTaskKilled(pTaskInfo);
×
1619
  if (isKilled) {
×
1620
    // clearStreamBlock(pTaskInfo->pRoot);
1621
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
×
1622

1623
    taosRUnLockLatch(&pTaskInfo->lock);
×
1624
    return pTaskInfo->code;
×
1625
  }
1626

1627
  if (pTaskInfo->owner != 0) {
×
1628
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
×
1629
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
×
1630

1631
    taosRUnLockLatch(&pTaskInfo->lock);
×
1632
    return pTaskInfo->code;
×
1633
  }
1634

1635
  pTaskInfo->owner = threadId;
×
1636
  taosRUnLockLatch(&pTaskInfo->lock);
×
1637

1638
  if (pTaskInfo->cost.start == 0) {
×
1639
    pTaskInfo->cost.start = taosGetTimestampUs();
×
1640
  }
1641

1642
  // error occurs, record the error code and return to client
1643
  int32_t ret = setjmp(pTaskInfo->env);
×
1644
  if (ret != TSDB_CODE_SUCCESS) {
×
1645
    pTaskInfo->code = ret;
×
1646
    (void)cleanUpUdfs();
×
1647
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
×
1648
    atomic_store_64(&pTaskInfo->owner, 0);
×
1649
    return pTaskInfo->code;
×
1650
  }
1651

1652
  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));
×
1653

1654
  int64_t st = taosGetTimestampUs();
×
1655

1656
  int32_t code = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot, ppRes);
×
1657
  if (code) {
×
1658
    pTaskInfo->code = code;
×
1659
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1660
  } else {
1661
    *finished = *ppRes == NULL;
×
1662
    code = blockDataCheck(*ppRes);
×
1663
  }
1664
  if (code) {
×
1665
    pTaskInfo->code = code;
×
1666
    qError("%s failed at line %d, code:%s %s", __func__, __LINE__, tstrerror(code), GET_TASKID(pTaskInfo));
×
1667
  }
1668

1669
  uint64_t el = (taosGetTimestampUs() - st);
×
1670

1671
  pTaskInfo->cost.elapsedTime += el;
×
1672
  if (NULL == *ppRes) {
×
1673
    *useconds = pTaskInfo->cost.elapsedTime;
×
1674
  }
1675

1676
  (void)cleanUpUdfs();
×
1677

1678
  int32_t  current = (*ppRes != NULL) ? (*ppRes)->info.rows : 0;
×
1679
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
×
1680

1681
  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
×
1682
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);
1683

1684
  atomic_store_64(&pTaskInfo->owner, 0);
×
1685
  return pTaskInfo->code;
×
1686
}
1687

1688
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
1689
//   SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1690
//   pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
1691
// }
1692

1693
int32_t qStreamCreateTableListForReader(void* pVnode, uint64_t suid, uint64_t uid, int8_t tableType,
×
1694
                                        SNodeList* pGroupTags, bool groupSort, SNode* pTagCond, SNode* pTagIndexCond,
1695
                                        SStorageAPI* storageAPI, void** pTableListInfo, SHashObj* groupIdMap) {
1696
  STableListInfo* pList = tableListCreate();
×
1697
  if (pList == NULL) {
×
1698
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1699
    return terrno;
×
1700
  }
1701

1702
  SScanPhysiNode pScanNode = {.suid = suid, .uid = uid, .tableType = tableType};
×
1703
  SReadHandle    pHandle = {.vnode = pVnode};
×
1704
  SExecTaskInfo  pTaskInfo = {.id.str = "", .storageAPI = *storageAPI};
×
1705

1706
  int32_t code = createScanTableListInfo(&pScanNode, pGroupTags, groupSort, &pHandle, pList, pTagCond, pTagIndexCond,
×
1707
                                         &pTaskInfo, groupIdMap);
1708
  if (code != 0) {
×
1709
    tableListDestroy(pList);
×
1710
    qError("failed to createScanTableListInfo, code:%s", tstrerror(code));
×
1711
    return code;
×
1712
  }
1713
  *pTableListInfo = pList;
×
1714
  return 0;
×
1715
}
1716

1717
int32_t qStreamGetTableList(void* pTableListInfo, int32_t currentGroupId, STableKeyInfo** pKeyInfo, int32_t* size) {
×
1718
  if (pTableListInfo == NULL || pKeyInfo == NULL || size == NULL) {
×
1719
    return TSDB_CODE_INVALID_PARA;
×
1720
  }
1721
  if (currentGroupId == -1) {
×
1722
    *size = taosArrayGetSize(((STableListInfo*)pTableListInfo)->pTableList);
×
1723
    *pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, 0);
×
1724
    return 0;
×
1725
  }
1726
  return tableListGetGroupList(pTableListInfo, currentGroupId, pKeyInfo, size);
×
1727
}
1728

1729
int32_t  qStreamSetTableList(void** pTableListInfo, STableKeyInfo* data){
×
1730
  if (*pTableListInfo == NULL) {
×
1731
    *pTableListInfo = tableListCreate();
×
1732
    if (*pTableListInfo == NULL) {
×
1733
      return terrno;
×
1734
    }
1735
  }
1736
  return taosArrayPush(((STableListInfo*)(*pTableListInfo))->pTableList, data) != NULL ? 0 : terrno;
×
1737
}
1738

1739
int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid) {
×
1740
  for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
×
1741
    int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
×
1742

1743
    STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
×
1744
    if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
×
1745
      return i;
×
1746
    }
1747
  }
1748
  return -1;
×
1749
}
1750

1751
void qStreamDestroyTableList(void* pTableListInfo) { tableListDestroy(pTableListInfo); }
×
1752

1753
uint64_t qStreamGetGroupId(void* pTableListInfo, int64_t uid) { return tableListGetTableGroupId(pTableListInfo, uid); }
×
1754

1755
int32_t qStreamGetTableListGroupNum(const void* pTableList) { return ((STableListInfo*)pTableList)->numOfOuputGroups; }
×
1756
SArray* qStreamGetTableArrayList(const void* pTableList) { return ((STableListInfo*)pTableList)->pTableList; }
×
1757

1758
int32_t qStreamFilter(SSDataBlock* pBlock, void* pFilterInfo) { return doFilter(pBlock, pFilterInfo, NULL); }
×
1759

1760
bool qStreamUidInTableList(void* pTableListInfo, uint64_t uid) {
×
1761
  return tableListGetTableGroupId(pTableListInfo, uid) != -1;
×
1762
}
1763

1764
void streamDestroyExecTask(qTaskInfo_t tInfo) {
×
1765
  qDestroyTask(tInfo);
×
1766
}
×
1767

1768
int32_t streamCalcOneScalarExpr(SNode* pExpr, SScalarParam* pDst, const SStreamRuntimeFuncInfo* pExtraParams) {
×
1769
  return streamCalcOneScalarExprInRange(pExpr, pDst, -1, -1, pExtraParams);
×
1770
}
1771

1772
int32_t streamCalcOneScalarExprInRange(SNode* pExpr, SScalarParam* pDst, int32_t rowStartIdx, int32_t rowEndIdx, const SStreamRuntimeFuncInfo* pExtraParams) {
×
1773
  int32_t      code = 0;
×
1774
  SNode*       pNode = 0;
×
1775
  SNodeList*   pList = NULL;
×
1776
  SExprInfo*   pExprInfo = NULL;
×
1777
  int32_t      numOfExprs = 1;
×
1778
  int32_t*     offset = 0;
×
1779
  STargetNode* pTargetNode = NULL;
×
1780
  code = nodesMakeNode(QUERY_NODE_TARGET, (SNode**)&pTargetNode);
×
1781
  if (code == 0) code = nodesCloneNode(pExpr, &pNode);
×
1782

1783
  if (code == 0) {
×
1784
    pTargetNode->dataBlockId = 0;
×
1785
    pTargetNode->pExpr = pNode;
×
1786
    pTargetNode->slotId = 0;
×
1787
  }
1788
  if (code == 0) {
×
1789
    code = nodesMakeList(&pList);
×
1790
  }
1791
  if (code == 0) {
×
1792
    code = nodesListAppend(pList, (SNode*)pTargetNode);
×
1793
  }
1794
  if (code == 0) {
×
1795
    pNode = NULL;
×
1796
    code = createExprInfo(pList, NULL, &pExprInfo, &numOfExprs);
×
1797
  }
1798

1799
  if (code == 0) {
×
1800
    const char* pVal = NULL;
×
1801
    int32_t     len = 0;
×
1802
    SNode*      pSclNode = NULL;
×
1803
    switch (pExprInfo->pExpr->nodeType) {
×
1804
      case QUERY_NODE_FUNCTION:
×
1805
        pSclNode = (SNode*)pExprInfo->pExpr->_function.pFunctNode;
×
1806
        break;
×
1807
      case QUERY_NODE_OPERATOR:
×
1808
        pSclNode = pExprInfo->pExpr->_optrRoot.pRootNode;
×
1809
        break;
×
1810
      default:
×
1811
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1812
        break;
×
1813
    }
1814
    SArray*     pBlockList = taosArrayInit(2, POINTER_BYTES);
×
1815
    SSDataBlock block = {0};
×
1816
    block.info.rows = 1;
×
1817
    SSDataBlock* pBlock = &block;
×
1818
    taosArrayPush(pBlockList, &pBlock);
1819
    if (code == 0) code = scalarCalculateInRange(pSclNode, pBlockList, pDst, rowStartIdx, rowEndIdx, pExtraParams, NULL);
×
1820
    taosArrayDestroy(pBlockList);
×
1821
  }
1822
  nodesDestroyList(pList);
×
1823
  destroyExprInfo(pExprInfo, numOfExprs);
×
1824
  taosMemoryFreeClear(pExprInfo);
×
1825
  return code;
×
1826
}
1827

1828
int32_t streamForceOutput(qTaskInfo_t tInfo, SSDataBlock** pRes, int32_t winIdx) {
×
1829
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tInfo;
×
1830
  const SArray*  pForceOutputCols = pTaskInfo->pStreamRuntimeInfo->pForceOutputCols;
×
1831
  int32_t        code = 0;
×
1832
  SNode*         pNode = NULL;
×
1833
  if (!pForceOutputCols) return 0;
×
1834
  if (!*pRes) {
×
1835
    code = createDataBlock(pRes);
×
1836
  }
1837

1838
  if (code == 0 && (!(*pRes)->pDataBlock || (*pRes)->pDataBlock->size == 0)) {
×
1839
    int32_t idx = 0;
×
1840
    for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
×
1841
      SStreamOutCol*  pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
×
1842
      SColumnInfoData colInfo = createColumnInfoData(pCol->type.type, pCol->type.bytes, idx++);
×
1843
      colInfo.info.precision = pCol->type.precision;
×
1844
      colInfo.info.scale = pCol->type.scale;
×
1845
      code = blockDataAppendColInfo(*pRes, &colInfo);
×
1846
      if (code != 0) break;
×
1847
    }
1848
  }
1849

1850
  blockDataEnsureCapacity(*pRes, (*pRes)->info.rows + 1);
×
1851

1852
  // loop all exprs for force output, execute all exprs
1853
  int32_t idx = 0;
×
1854
  int32_t rowIdx = (*pRes)->info.rows;
×
1855
  int32_t tmpWinIdx = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
×
1856
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = winIdx;
×
1857
  for (int32_t i = 0; i < pForceOutputCols->size; ++i) {
×
1858
    SScalarParam   dst = {0};
×
1859
    SStreamOutCol* pCol = (SStreamOutCol*)taosArrayGet(pForceOutputCols, i);
×
1860
    code = nodesStringToNode(pCol->expr, &pNode);
×
1861
    if (code != 0) break;
×
1862
    SColumnInfoData* pInfo = taosArrayGet((*pRes)->pDataBlock, idx);
×
1863
    if (nodeType(pNode) == QUERY_NODE_VALUE) {
×
1864
      void* p = nodesGetValueFromNode((SValueNode*)pNode);
×
1865
      code = colDataSetVal(pInfo, rowIdx, p, ((SValueNode*)pNode)->isNull);
×
1866
    } else {
1867
      dst.columnData = pInfo;
×
1868
      dst.numOfRows = rowIdx;
×
1869
      dst.colAlloced = false;
×
1870
      code = streamCalcOneScalarExprInRange(pNode, &dst, rowIdx,  rowIdx, &pTaskInfo->pStreamRuntimeInfo->funcInfo);
×
1871
    }
1872
    ++idx;
×
1873
    // TODO sclFreeParam(&dst);
1874
    nodesDestroyNode(pNode);
×
1875
    if (code != 0) break;
×
1876
  }
1877
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = tmpWinIdx;
×
1878
  (*pRes)->info.rows++;
×
1879
  return code;
×
1880
}
1881

1882
int32_t streamCalcOutputTbName(SNode* pExpr, char* tbname, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
×
1883
  int32_t      code = 0;
×
1884
  const char*  pVal = NULL;
×
1885
  SScalarParam dst = {0};
×
1886
  int32_t      len = 0;
×
1887
  // execute the expr
1888
  switch (pExpr->type) {
×
1889
    case QUERY_NODE_VALUE: {
×
1890
      SValueNode* pValue = (SValueNode*)pExpr;
×
1891
      int32_t     type = pValue->node.resType.type;
×
1892
      if (!IS_STR_DATA_TYPE(type)) {
×
1893
        qError("invalid sub tb expr with non-str type");
×
1894
        code = TSDB_CODE_INVALID_PARA;
×
1895
        break;
×
1896
      }
1897
      void* pTmp = nodesGetValueFromNode((SValueNode*)pExpr);
×
1898
      if (pTmp == NULL) {
×
1899
        qError("invalid sub tb expr with null value");
×
1900
        code = TSDB_CODE_INVALID_PARA;
×
1901
        break;
×
1902
      }
1903
      pVal = varDataVal(pTmp);
×
1904
      len = varDataLen(pTmp);
×
1905
    } break;
×
1906
    case QUERY_NODE_FUNCTION: {
×
1907
      SFunctionNode* pFunc = (SFunctionNode*)pExpr;
×
1908
      if (!IS_STR_DATA_TYPE(pFunc->node.resType.type)) {
×
1909
        qError("invalid sub tb expr with non-str type func");
×
1910
        code = TSDB_CODE_INVALID_PARA;
×
1911
        break;
×
1912
      }
1913
      SColumnInfoData* pCol = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
1914
      if (!pCol) {
×
1915
        code = terrno;
×
1916
        qError("failed to allocate col info data at: %s, %d", __func__, __LINE__);
×
1917
        break;
×
1918
      }
1919

1920
      pCol->hasNull = true;
×
1921
      pCol->info.type = ((SExprNode*)pExpr)->resType.type;
×
1922
      pCol->info.colId = 0;
×
1923
      pCol->info.bytes = ((SExprNode*)pExpr)->resType.bytes;
×
1924
      pCol->info.precision = ((SExprNode*)pExpr)->resType.precision;
×
1925
      pCol->info.scale = ((SExprNode*)pExpr)->resType.scale;
×
1926
      colInfoDataEnsureCapacity(pCol, 1, true);
×
1927
      dst.columnData = pCol;
×
1928
      dst.numOfRows = 1;
×
1929
      dst.colAlloced = true;
×
1930
      code = streamCalcOneScalarExpr(pExpr, &dst, pStreamRuntimeInfo);
×
1931
      if (code == 0) {
×
1932
        pVal = varDataVal(colDataGetVarData(dst.columnData, 0));
×
1933
        len = varDataLen(colDataGetVarData(dst.columnData, 0));
×
1934
      }
1935
    } break;
×
1936
    default:
×
1937
      qError("wrong subtable expr with type: %d", pExpr->type);
×
1938
      code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1939
      break;
×
1940
  }
1941
  if (code == 0) {
×
1942
    if (!pVal || len == 0) {
×
1943
      qError("tbname generated with no characters which is not allowed");
×
1944
      code = TSDB_CODE_INVALID_PARA;
×
1945
    }
1946
    if(len > TSDB_TABLE_NAME_LEN - 1) {
×
1947
      qError("tbname generated with too long characters, max allowed is %d, got %d", TSDB_TABLE_NAME_LEN - 1, len);
×
1948
      code = TSDB_CODE_MND_STREAM_TBNAME_TOO_LONG;
×
1949
    }
1950
    memcpy(tbname, pVal, len);
×
1951
  }
1952
  // TODO free dst
1953
  sclFreeParam(&dst);
×
1954
  return code;
×
1955
}
1956

1957
void destroyStreamInserterParam(SStreamInserterParam* pParam) {
×
1958
  if (pParam) {
×
1959
    if (pParam->tbname) {
×
1960
      taosMemFree(pParam->tbname);
×
1961
      pParam->tbname = NULL;
×
1962
    }
1963
    if (pParam->stbname) {
×
1964
      taosMemFree(pParam->stbname);
×
1965
      pParam->stbname = NULL;
×
1966
    }
1967
    if (pParam->dbFName) {
×
1968
      taosMemFree(pParam->dbFName);
×
1969
      pParam->dbFName = NULL;
×
1970
    }
1971
    if (pParam->pFields) {
×
1972
      taosArrayDestroy(pParam->pFields);
×
1973
      pParam->pFields = NULL;
×
1974
    }
1975
    if (pParam->pTagFields) {
×
1976
      taosArrayDestroy(pParam->pTagFields);
×
1977
      pParam->pTagFields = NULL;
×
1978
    }
1979
    if (pParam->pSchema) {
×
1980
      taosMemFree(pParam->pSchema);
×
1981
      pParam->pSchema = NULL;
×
1982
    }
1983
    taosMemFree(pParam);
×
1984
  }
1985
}
×
1986

1987
int32_t cloneStreamInserterParam(SStreamInserterParam** ppDst, SStreamInserterParam* pSrc) {
×
1988
  int32_t code = 0, lino = 0;
×
1989
  if (ppDst == NULL || pSrc == NULL) {
×
1990
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
×
1991
  }
1992
  *ppDst = (SStreamInserterParam*)taosMemoryCalloc(1, sizeof(SStreamInserterParam));
×
1993
  TSDB_CHECK_NULL(*ppDst, code, lino, _exit, terrno);
×
1994

1995
  (*ppDst)->suid = pSrc->suid;
×
1996
  (*ppDst)->sver = pSrc->sver;
×
1997
  (*ppDst)->tbType = pSrc->tbType;
×
1998
  (*ppDst)->tbname = taosStrdup(pSrc->tbname);
×
1999
  TSDB_CHECK_NULL((*ppDst)->tbname, code, lino, _exit, terrno);
×
2000

2001
  if (pSrc->stbname) {
×
2002
    (*ppDst)->stbname = taosStrdup(pSrc->stbname);
×
2003
    TSDB_CHECK_NULL((*ppDst)->stbname, code, lino, _exit, terrno);
×
2004
  }
2005

2006
  (*ppDst)->dbFName = taosStrdup(pSrc->dbFName);
×
2007
  TSDB_CHECK_NULL((*ppDst)->dbFName, code, lino, _exit, terrno);
×
2008

2009
  (*ppDst)->pSinkHandle = pSrc->pSinkHandle;  // don't need clone and free
×
2010

2011
  if (pSrc->pFields && pSrc->pFields->size > 0) {
×
2012
    (*ppDst)->pFields = taosArrayDup(pSrc->pFields, NULL);
×
2013
    TSDB_CHECK_NULL((*ppDst)->pFields, code, lino, _exit, terrno);
×
2014
  } else {
2015
    (*ppDst)->pFields = NULL;
×
2016
  }
2017
  
2018
  if (pSrc->pTagFields && pSrc->pTagFields->size > 0) {
×
2019
    (*ppDst)->pTagFields = taosArrayDup(pSrc->pTagFields, NULL);
×
2020
    TSDB_CHECK_NULL((*ppDst)->pTagFields, code, lino, _exit, terrno);
×
2021
  } else {
2022
    (*ppDst)->pTagFields = NULL;
×
2023
  }
2024

2025
_exit:
×
2026

2027
  if (code != 0) {
×
2028
    if (*ppDst) {
×
2029
      destroyStreamInserterParam(*ppDst);
×
2030
      *ppDst = NULL;
×
2031
    }
2032
    
2033
    stError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2034
  }
2035
  return code;
×
2036
}
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