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

taosdata / TDengine / #5073

17 May 2026 01:15AM UTC coverage: 73.317% (+0.02%) from 73.298%
#5073

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281387 of 383795 relevant lines covered (73.32%)

138791003.92 hits per line

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

85.14
/source/dnode/vnode/src/vnd/vnodeStream.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 <stdbool.h>
17
#include <stdint.h>
18
#include <taos.h>
19
#include <tdef.h>
20
#include "executor.h"
21
#include "nodes.h"
22
#include "osMemPool.h"
23
#include "osMemory.h"
24
#include "scalar.h"
25
#include "stream.h"
26
#include "streamReader.h"
27
#include "taosdef.h"
28
#include "taoserror.h"
29
#include "tarray.h"
30
#include "tcommon.h"
31
#include "tdatablock.h"
32
#include "tdb.h"
33
#include "tdef.h"
34
#include "tencode.h"
35
#include "tglobal.h"
36
#include "thash.h"
37
#include "tlist.h"
38
#include "tlockfree.h"
39
#include "tmsg.h"
40
#include "tsimplehash.h"
41
#include "ttypes.h"
42
#include "vnd.h"
43
#include "vnode.h"
44
#include "vnodeInt.h"
45
#include "executor.h"
46

47
int32_t cacheTag(SVnode* pVnode, SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr, SStorageAPI* api, uint64_t uid, col_id_t colId, SRWLatch* lock);
48

49
#define BUILD_OPTION(options, _suid, _ver, _order, startTime, endTime, _schemas, _isSchema, _pSlotList)      \
50
  SStreamOptions                       options = {.suid = _suid,                                                   \
51
                                                  .ver = _ver,                                                     \
52
                                                  .order = _order,                                                 \
53
                                                  .twindows = {.skey = startTime, .ekey = endTime},                \
54
                                                  .schemas = _schemas,                                             \
55
                                                  .isSchema = _isSchema,                                           \
56
                                                  .pSlotList = _pSlotList};
57

58
typedef struct WalMetaResult {
59
  uint64_t    id;
60
  int64_t     skey;
61
  int64_t     ekey;
62
} WalMetaResult;
63

64
static int64_t getSuid(SStreamTriggerReaderInfo* sStreamReaderInfo, STableKeyInfo* pList) {
8,196,281✔
65
  int64_t suid = 0;
8,196,281✔
66
  if (!sStreamReaderInfo->isVtableStream) {
8,196,281✔
67
    suid = sStreamReaderInfo->suid;
7,729,348✔
68
    goto end;
7,729,348✔
69
  }
70

71
  if (pList == NULL) {
466,933✔
72
    goto end;
×
73
  }
74

75
  taosRLockLatch(&sStreamReaderInfo->lock);
466,933✔
76
  SStreamTableMapElement* element = taosHashGet(sStreamReaderInfo->vSetTableList.uIdMap, &pList->uid, LONG_BYTES);  
466,933✔
77
  if (element != 0) {
466,933✔
78
    suid = element->table->groupId;
268,966✔
79
    taosRUnLockLatch(&sStreamReaderInfo->lock);
268,966✔
80
    goto end;
268,966✔
81
  }
82
  taosRUnLockLatch(&sStreamReaderInfo->lock);
197,967✔
83

84
end:
8,196,281✔
85
  return suid;
8,196,281✔
86
}
87

88
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
8,277,167✔
89

90
int32_t sortCid(const void *lp, const void *rp) {
2,618,212✔
91
  int16_t* c1 = (int16_t*)lp;
2,618,212✔
92
  int16_t* c2 = (int16_t*)rp;
2,618,212✔
93

94
  if (*c1 < *c2) {
2,618,212✔
95
    return -1;
2,601,998✔
96
  } else if (*c1 > *c2) {
16,214✔
97
    return 1;
16,214✔
98
  }
99

100
  return 0;
×
101
}
102

103
int32_t sortSSchema(const void *lp, const void *rp) {
2,604,660✔
104
  SSchema* c1 = (SSchema*)lp;
2,604,660✔
105
  SSchema* c2 = (SSchema*)rp;
2,604,660✔
106

107
  if (c1->colId < c2->colId) {
2,604,660✔
108
    return -1;
2,588,446✔
109
  } else if (c1->colId > c2->colId) {
16,214✔
110
    return 1;
16,214✔
111
  }
112

113
  return 0;
×
114
}
115

116
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
45,944,908✔
117
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
45,944,908✔
118
  if (pSrc == NULL) {
45,951,026✔
119
    return terrno;
×
120
  }
121

122
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
45,951,026✔
123
  return 0;
45,948,002✔
124
}
125

126
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
11,180,759✔
127
  int32_t code = pTask->storageApi->tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
11,180,759✔
128
  if (code != TSDB_CODE_SUCCESS) {
11,181,258✔
129
    pTask->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
130
  }
131

132
  return code;
11,179,999✔
133
}
134

135
static int32_t getTableData(SStreamReaderTaskInner* pTask, SSDataBlock** ppRes) {
1,945,484✔
136
  return pTask->storageApi->tsdReader.tsdReaderRetrieveDataBlock(pTask->pReader, ppRes);
1,945,484✔
137
}
138

139
static int32_t buildOTableInfoRsp(const SSTriggerOrigTableInfoRsp* rsp, void** data, size_t* size) {
149,650✔
140
  int32_t code = 0;
149,650✔
141
  int32_t lino = 0;
149,650✔
142
  void*   buf = NULL;
149,650✔
143
  int32_t len = tSerializeSTriggerOrigTableInfoRsp(NULL, 0, rsp);
149,650✔
144
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
149,650✔
145
  buf = rpcMallocCont(len);
149,650✔
146
  STREAM_CHECK_NULL_GOTO(buf, terrno);
149,650✔
147
  int32_t actLen = tSerializeSTriggerOrigTableInfoRsp(buf, len, rsp);
149,650✔
148
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
149,650✔
149
  *data = buf;
149,650✔
150
  *size = len;
149,650✔
151
  buf = NULL;
149,650✔
152
end:
149,650✔
153
  rpcFreeCont(buf);
149,650✔
154
  return code;
149,650✔
155
}
156

157
static bool ignoreMetaChange(int64_t tableListVer, int64_t ver) {
229,379✔
158
  stDebug("%s tableListVer:%" PRId64 " ver:%" PRId64, __func__, tableListVer, ver);
229,379✔
159
  return tableListVer >= ver;
229,379✔
160
}
161

162
static bool needReLoadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
6,255,384✔
163
  if ((tableType == TD_CHILD_TABLE || tableType == TD_VIRTUAL_CHILD_TABLE) &&
6,255,384✔
164
      sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
2,848,352✔
165
      suid == sStreamReaderInfo->suid) {
1,048,800✔
166
    taosRLockLatch(&sStreamReaderInfo->lock);
13,356✔
167
    uint64_t gid = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
13,356✔
168
    taosRUnLockLatch(&sStreamReaderInfo->lock);
13,356✔
169
    if (gid == (uint64_t)-1) return true;
13,356✔
170
  }
171
  return false;
6,242,642✔
172
}
173

174
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id){
14,533,414✔
175
  int32_t  ret = false;
14,533,414✔
176
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
14,533,414✔
177
    if (suid != sStreamReaderInfo->suid) goto end;
8,307,161✔
178
    if (qStreamGetTableListNum(sStreamReaderInfo) == 0) goto end;
3,997,331✔
179
  } 
180
  *id = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
10,220,958✔
181
  if (*id == -1) goto end;
10,236,716✔
182
  ret = true;
6,311,356✔
183

184
end:
14,562,234✔
185
  stTrace("%s ret:%d %p %p check suid:%" PRId64 " uid:%" PRId64 " gid:%"PRIu64, __func__, ret, sStreamReaderInfo, sStreamReaderInfo->tableList.gIdMap, suid, uid, *id);
14,562,234✔
186
  return ret;
14,547,283✔
187
}
188

189
static bool uidInTableListOrigin(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
37,790✔
190
  return uidInTableList(sStreamReaderInfo, suid, uid, id);
37,790✔
191
}
192

193
static bool uidInTableListSet(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc) {
64,380,509✔
194
  bool ret = false;
64,380,509✔
195
  taosRLockLatch(&sStreamReaderInfo->lock);
64,380,509✔
196
  if (sStreamReaderInfo->isVtableStream) {
64,399,377✔
197
    int64_t tmp[2] = {suid, uid};
49,896,466✔
198
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) != NULL) {
49,896,708✔
199
      *id = uid;
16,478,667✔
200
      ret = true;
16,478,940✔
201
    }
202
  } else {
203
    ret = uidInTableList(sStreamReaderInfo, suid, uid, id);
14,503,776✔
204
  }
205

206
end:
64,399,294✔
207
  taosRUnLockLatch(&sStreamReaderInfo->lock);
64,399,294✔
208
  return ret;
64,417,253✔
209
}
210

211
static int32_t  qTransformStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo, StreamTableListInfo* tableInfo){
371,990✔
212
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
371,990✔
213
  int32_t totalSize = taosArrayGetSize(pList);
371,990✔
214
  int32_t code = 0;
371,990✔
215
  void* pTask = sStreamReaderInfo->pTask;
371,990✔
216
  for (int32_t i = 0; i < totalSize; ++i) {
1,004,351✔
217
    STableKeyInfo* info = taosArrayGet(pList, i);
632,361✔
218
    if (info == NULL) {
632,361✔
219
      continue;
×
220
    }
221
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
632,361✔
222
    if (code != 0){
631,917✔
223
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
224
      continue;
×
225
    }
226
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag, sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
631,917✔
227
    if (code != 0){
632,070✔
228
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
229
      continue;
×
230
    }
231
    code = qStreamSetTableList(tableInfo, info->uid, info->groupId);
632,070✔
232
    if (code != 0){
632,361✔
233
      return code;
×
234
    }
235
  }
236
  return 0;
371,990✔
237
}
238

239
static int32_t generateTablistForStreamReader(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo) {
371,246✔
240
  int32_t                   code = 0;
371,246✔
241
  int32_t                   lino = 0;
371,246✔
242
  SNodeList* groupNew = NULL;   
371,246✔
243
  void* pTableListInfo = NULL;
371,990✔
244

245
  
246
  STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
371,990✔
247

248
  STREAM_CHECK_RET_GOTO(qStreamCreateTableListForReader(pVnode, sStreamReaderInfo->suid, sStreamReaderInfo->uid, sStreamReaderInfo->tableType, groupNew,
371,990✔
249
                                         true, sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond, &sStreamReaderInfo->storageApi, 
250
                                         &pTableListInfo, sStreamReaderInfo->groupIdMap));
251
  
252
  STREAM_CHECK_RET_GOTO(qTransformStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
371,990✔
253
  
254
  void* pTask = sStreamReaderInfo->pTask;
371,990✔
255
  ST_TASK_DLOG("vgId:%d %s tablelist size:%" PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(sStreamReaderInfo->tableList.pTableList));
371,990✔
256
end:
371,511✔
257
  nodesDestroyList(groupNew);
371,990✔
258
  qStreamDestroyTableList(pTableListInfo);
371,990✔
259
  STREAM_PRINT_LOG_END(code, lino);
371,990✔
260
  return code;
371,990✔
261
}
262

263
static int32_t buildVTableInfoRsp(const SStreamMsgVTableInfo* rsp, void** data, size_t* size) {
87,716✔
264
  int32_t code = 0;
87,716✔
265
  int32_t lino = 0;
87,716✔
266
  void*   buf = NULL;
87,716✔
267
  int32_t len = tSerializeSStreamMsgVTableInfo(NULL, 0, rsp);
87,716✔
268
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
87,716✔
269
  buf = rpcMallocCont(len);
87,716✔
270
  STREAM_CHECK_NULL_GOTO(buf, terrno);
87,716✔
271
  int32_t actLen = tSerializeSStreamMsgVTableInfo(buf, len, rsp);
87,716✔
272
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
87,716✔
273
  *data = buf;
87,716✔
274
  *size = len;
87,716✔
275
  buf = NULL;
87,716✔
276
end:
87,716✔
277
  rpcFreeCont(buf);
87,716✔
278
  return code;
87,716✔
279
}
280

281
static int32_t buildTsRsp(const SStreamTsResponse* tsRsp, void** data, size_t* size) {
727,337✔
282
  int32_t code = 0;
727,337✔
283
  int32_t lino = 0;
727,337✔
284
  void*   buf = NULL;
727,337✔
285
  int32_t len = tSerializeSStreamTsResponse(NULL, 0, tsRsp);
727,337✔
286
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
727,065✔
287
  buf = rpcMallocCont(len);
727,065✔
288
  STREAM_CHECK_NULL_GOTO(buf, terrno);
726,873✔
289
  int32_t actLen = tSerializeSStreamTsResponse(buf, len, tsRsp);
726,873✔
290
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
727,337✔
291
  *data = buf;
727,337✔
292
  *size = len;
727,337✔
293
  buf = NULL;
727,337✔
294
end:
727,337✔
295
  rpcFreeCont(buf);
727,337✔
296
  return code;
727,065✔
297
}
298

299

300
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
10,077,674✔
301
  int32_t code = 0;
10,077,674✔
302
  int32_t lino = 0;
10,077,674✔
303
  void*   buf = NULL;
10,077,674✔
304
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
10,077,674✔
305
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
2,842,046✔
306
  buf = rpcMallocCont(dataEncodeSize);
2,842,046✔
307
  STREAM_CHECK_NULL_GOTO(buf, terrno);
2,842,046✔
308
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
2,842,046✔
309
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
2,842,046✔
310
  *data = buf;
2,842,046✔
311
  *size = dataEncodeSize;
2,842,046✔
312
  buf = NULL;
2,842,046✔
313
end:
10,083,243✔
314
  rpcFreeCont(buf);
10,083,243✔
315
  return code;
10,079,132✔
316
}
317

318
static int32_t buildArrayRsp(SArray* pBlockList, void** data, size_t* size) {
155,891✔
319
  int32_t code = 0;
155,891✔
320
  int32_t lino = 0;
155,891✔
321

322
  void*   buf = NULL;
155,891✔
323

324
  int32_t blockNum = 0;
155,891✔
325
  size_t  dataEncodeBufSize = 0;
155,891✔
326
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
364,945✔
327
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
209,054✔
328
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
209,054✔
329
    int32_t blockSize = blockGetEncodeSize(pBlock);
209,054✔
330
    dataEncodeBufSize += blockSize;
209,054✔
331
    blockNum++;
209,054✔
332
  }
333
  buf = rpcMallocCont(INT_BYTES + dataEncodeBufSize);
155,891✔
334
  STREAM_CHECK_NULL_GOTO(buf, terrno);
155,891✔
335

336
  char* dataBuf = (char*)buf;
155,891✔
337
  *((int32_t*)(dataBuf)) = blockNum;
155,891✔
338
  dataBuf += INT_BYTES;
155,891✔
339
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
364,945✔
340
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
209,054✔
341
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
209,054✔
342
    int32_t actualLen = blockEncode(pBlock, dataBuf, dataEncodeBufSize, taosArrayGetSize(pBlock->pDataBlock));
209,054✔
343
    STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
209,054✔
344
    dataBuf += actualLen;
209,054✔
345
  }
346
  *data = buf;
155,891✔
347
  *size = INT_BYTES + dataEncodeBufSize;
155,891✔
348
  buf = NULL;
155,891✔
349
end:
155,891✔
350
  rpcFreeCont(buf);
155,891✔
351
  return code;
155,891✔
352
}
353

354
static int32_t buildWalMetaBlock(SSDataBlock* pBlock, int8_t type, int64_t id, bool isVTable, int64_t uid,
×
355
                                 int64_t skey, int64_t ekey, int64_t ver, int64_t rows) {
356
  int32_t code = 0;
×
357
  int32_t lino = 0;
×
358
  int32_t index = 0;
×
359
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
×
360
  if (!isVTable) {
×
361
    STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
×
362
  }
363
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &uid));
×
364
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
×
365
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
×
366
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
×
367
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &rows));
×
368

369
end:
×
370
  // STREAM_PRINT_LOG_END(code, lino)
371
  return code;
×
372
}
373

374
static int32_t buildWalMetaBlockNew(SSDataBlock* pBlock, int64_t id, int64_t skey, int64_t ekey, int64_t ver) {
11,092,949✔
375
  int32_t code = 0;
11,092,949✔
376
  int32_t lino = 0;
11,092,949✔
377
  int32_t index = 0;
11,092,949✔
378
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
11,092,949✔
379
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
11,094,349✔
380
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
11,093,858✔
381
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
11,092,297✔
382

383
end:
11,093,606✔
384
  return code;
11,093,606✔
385
}
386

387
static int32_t buildTableBlock(SSDataBlock* pBlock, int64_t id, int64_t ver, ETableBlockType type) {
3,814✔
388
  int32_t code = 0;
3,814✔
389
  int32_t lino = 0;
3,814✔
390
  int32_t index = 0;
3,814✔
391
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
3,814✔
392
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
3,814✔
393
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
3,814✔
394

395
end:
3,814✔
396
  return code;
3,814✔
397
}
398

399
static void buildTSchema(STSchema* pTSchema, int32_t ver, col_id_t colId, int8_t type, int32_t bytes) {
×
400
  pTSchema->numOfCols = 1;
×
401
  pTSchema->version = ver;
×
402
  pTSchema->columns[0].colId = colId;
×
403
  pTSchema->columns[0].type = type;
×
404
  pTSchema->columns[0].bytes = bytes;
×
405
}
×
406

407
static int32_t scanDeleteDataNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
34,740✔
408
                              int64_t ver) {
409
  int32_t    code = 0;
34,740✔
410
  int32_t    lino = 0;
34,740✔
411
  SDecoder   decoder = {0};
34,740✔
412
  SDeleteRes req = {0};
34,740✔
413
  void* pTask = sStreamReaderInfo->pTask;
34,740✔
414

415
  req.uidList = taosArrayInit(0, sizeof(tb_uid_t));
34,740✔
416
  tDecoderInit(&decoder, data, len);
34,740✔
417
  STREAM_CHECK_RET_GOTO(tDecodeDeleteRes(&decoder, &req));
34,740✔
418
  STREAM_CHECK_CONDITION_GOTO((sStreamReaderInfo->tableType == TSDB_SUPER_TABLE && !sStreamReaderInfo->isVtableStream && req.suid != sStreamReaderInfo->suid), TDB_CODE_SUCCESS);
34,740✔
419
  
420
  for (int32_t i = 0; i < taosArrayGetSize(req.uidList); i++) {
53,824✔
421
    uint64_t* uid = taosArrayGet(req.uidList, i);
30,612✔
422
    STREAM_CHECK_NULL_GOTO(uid, terrno);
30,885✔
423
    uint64_t   id = 0;
30,885✔
424
    ST_TASK_DLOG("stream reader scan delete start data:uid %" PRIu64 ", skey %" PRIu64 ", ekey %" PRIu64, *uid, req.skey, req.ekey);
30,885✔
425
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, req.suid, *uid, &id, false), TDB_CODE_SUCCESS);
30,885✔
426
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->deleteBlock, ((SSDataBlock*)rsp->deleteBlock)->info.rows + 1));
22,939✔
427
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->deleteBlock, id, req.skey, req.ekey, ver));
22,939✔
428
    ((SSDataBlock*)rsp->deleteBlock)->info.rows++;
22,939✔
429
    rsp->totalRows++;
22,939✔
430
  }
431

432
end:
34,740✔
433
  taosArrayDestroy(req.uidList);
34,740✔
434
  tDecoderClear(&decoder);
34,740✔
435
  return code;
34,740✔
436
}
437

438
static int32_t createBlockForProcessMeta(SSDataBlock** pBlock) {
3,268✔
439
  int32_t code = 0;
3,268✔
440
  int32_t lino = 0;
3,268✔
441
  SArray* schemas = NULL;
3,268✔
442

443
  schemas = taosArrayInit(8, sizeof(SSchema));
3,268✔
444
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
3,268✔
445

446
  int32_t index = 0;
3,268✔
447
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
3,268✔
448
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
3,268✔
449
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TINYINT, CHAR_BYTES, index++))  // type
3,268✔
450

451
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
3,268✔
452

453
end:
3,268✔
454
  taosArrayDestroy(schemas);
3,268✔
455
  return code;
3,268✔
456
}
457

458
static int32_t addOneRow(void** tmp, int64_t id, int64_t ver, ETableBlockType type) {
3,814✔
459
  int32_t  code = 0;
3,814✔
460
  int32_t  lino = 0;
3,814✔
461
  if (*tmp == NULL) {
3,814✔
462
    STREAM_CHECK_RET_GOTO(createBlockForProcessMeta((SSDataBlock**)tmp));
3,268✔
463
  }
464
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(*tmp, ((SSDataBlock*)(*tmp))->info.rows + 1));
3,814✔
465
  STREAM_CHECK_RET_GOTO(buildTableBlock(*tmp, id, ver, type));
3,814✔
466
  ((SSDataBlock*)(*tmp))->info.rows++;
3,814✔
467
  
468
end:
3,814✔
469
  return code;
3,814✔
470
}
471

472
static int32_t addUidListToBlock(SArray* uidListAdd, void** block, int64_t ver, int32_t* totalRows, ETableBlockType type) {
83,512✔
473
  for (int32_t i = 0; i < taosArrayGetSize(uidListAdd); ++i) {
87,326✔
474
    uint64_t* uid = taosArrayGet(uidListAdd, i);
3,814✔
475
    if (uid == NULL) {
3,814✔
476
      continue;
×
477
    }
478
    int32_t code = addOneRow(block, *uid, ver, type);
3,814✔
479
    if (code != 0) {
3,814✔
480
      return code;
×
481
    }
482
    (*totalRows)++;
3,814✔
483
  }
484
  return 0;
83,512✔
485
}
486

487
static int32_t qStreamGetAddTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* uidListAdd) {
102,989✔
488
  int32_t      code = 0;
102,989✔
489
  int32_t      lino = 0;
102,989✔
490
  if (uidListAdd == NULL) {
102,989✔
491
    return 0;
96,184✔
492
  }
493
  void* pTask = sStreamReaderInfo->pTask;
6,805✔
494
  
495
  taosRLockLatch(&sStreamReaderInfo->lock);
7,024✔
496
  int32_t totalSize = taosArrayGetSize(tableListAdd);
7,024✔
497
  for (int32_t i = 0; i < totalSize; ++i) {
9,264✔
498
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
2,240✔
499
    if (info == NULL) {
2,240✔
500
      continue;
×
501
    }
502
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, &info->uid, LONG_BYTES) != NULL) {
2,240✔
503
      continue;
×
504
    }
505
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &info->uid), terrno);
4,480✔
506
    ST_TASK_WLOG("%s real add table to list for uid:%" PRId64, __func__, info->uid);
2,240✔
507
  }
508

509
end:
7,024✔
510
  taosRUnLockLatch(&sStreamReaderInfo->lock);
7,024✔
511
  return code;
7,024✔
512
}
513

514
static int32_t qStreamGetDelTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListDel, SArray* uidListDel) {
61,831✔
515
  int32_t      code = 0;
61,831✔
516
  int32_t      lino = 0;
61,831✔
517
  if (uidListDel == NULL) {
61,831✔
518
    return 0;
×
519
  }
520
  void* pTask = sStreamReaderInfo->pTask;
61,831✔
521
  
522
  taosRLockLatch(&sStreamReaderInfo->lock);
61,831✔
523
  int32_t totalSize = taosArrayGetSize(tableListDel);
62,050✔
524
  for (int32_t i = 0; i < totalSize; ++i) {
63,080✔
525
    int64_t* uid = taosArrayGet(tableListDel, i);
1,030✔
526
    if (uid == NULL) {
1,030✔
527
      continue;
×
528
    }
529
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, uid, LONG_BYTES) == NULL) {
1,030✔
530
      continue;
×
531
    }
532
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDel, uid), terrno);
1,030✔
533
    ST_TASK_WLOG("%s real del table from list for uid:%" PRId64, __func__, *uid);
1,030✔
534
  }
535

536
end:
62,050✔
537
  taosRUnLockLatch(&sStreamReaderInfo->lock);
62,050✔
538
  return code;
62,050✔
539
}
540

541
static int32_t scanDropTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
37,246✔
542
                             int64_t ver) {
543
  int32_t  code = 0;
37,246✔
544
  int32_t  lino = 0;
37,246✔
545
  SDecoder decoder = {0};
37,246✔
546
  void* pTask = sStreamReaderInfo->pTask;
37,246✔
547
  SArray* uidList = NULL;
37,246✔
548
  SArray* uidListDel = NULL;
37,246✔
549
  SArray* uidListDelOutTbl = NULL;
37,246✔
550
  SVDropTbBatchReq req = {0};
37,246✔
551
  tDecoderInit(&decoder, data, len);
37,246✔
552
  STREAM_CHECK_RET_GOTO(tDecodeSVDropTbBatchReq(&decoder, &req));
37,246✔
553

554
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
74,492✔
555
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
37,246✔
556
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
37,246✔
557
    uint64_t id = 0;
37,246✔
558
    if(!uidInTableListOrigin(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id)) {
37,246✔
559
      continue;
36,216✔
560
    }
561

562
    if (sStreamReaderInfo->deleteOutTbl != 0) {
1,030✔
563
      if (uidListDelOutTbl == NULL) {
×
564
        uidListDelOutTbl = taosArrayInit(8, sizeof(tb_uid_t));
×
565
        STREAM_CHECK_NULL_GOTO(uidListDelOutTbl, terrno);
×
566
      }
567
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDelOutTbl, &pDropTbReq->uid), terrno);
×
568
    }
569
    if (sStreamReaderInfo->isVtableStream) {
1,030✔
570
      if (uidList == NULL) {
1,030✔
571
        uidList = taosArrayInit(8, sizeof(tb_uid_t));
1,030✔
572
        STREAM_CHECK_NULL_GOTO(uidList, terrno);
1,030✔
573
      }
574
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pDropTbReq->uid), terrno);
2,060✔
575
    }
576
    
577
    ST_TASK_DLOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
1,030✔
578
  }
579
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDelOutTbl, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_DROP));
37,246✔
580

581
  if (sStreamReaderInfo->isVtableStream) {
37,246✔
582
    uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
37,246✔
583
    STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
37,246✔
584
    STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
37,246✔
585
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
37,246✔
586
  }
587
  
588
end:
37,246✔
589
  taosArrayDestroy(uidList);
37,246✔
590
  taosArrayDestroy(uidListDel);
37,004✔
591
  taosArrayDestroy(uidListDelOutTbl);
37,246✔
592
  tDecoderClear(&decoder);
37,246✔
593
  return code;
37,246✔
594
}
595

596
static int32_t qStreamModifyTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* tableListDel) {
104,637✔
597
  int32_t      code = 0;
104,637✔
598
  int32_t      lino = 0;
104,637✔
599
  void* pTask = sStreamReaderInfo->pTask;
104,637✔
600
  
601
  taosWLockLatch(&sStreamReaderInfo->lock);
104,637✔
602
  int32_t totalSize = taosArrayGetSize(tableListDel);
104,856✔
603
  for (int32_t i = 0; i < totalSize; ++i) {
104,856✔
604
    int64_t* uid = taosArrayGet(tableListDel, i);
×
605
    if (uid == NULL) {
×
606
      continue;
×
607
    }
608
    STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, *uid));
×
609
  }
610

611
  totalSize = taosArrayGetSize(tableListAdd);
104,856✔
612
  for (int32_t i = 0; i < totalSize; ++i) {
141,788✔
613
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
36,713✔
614
    if (info == NULL) {
36,932✔
615
      continue;
×
616
    }
617
    int ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
36,932✔
618
    if (ret != 0){
36,932✔
619
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64",code:%d", __func__, info->uid, ret);
7,476✔
620
      continue;
7,476✔
621
    }
622
    ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag, sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
29,456✔
623
    if (ret != 0){
29,456✔
624
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64",code:%d", __func__, info->uid, ret);
×
625
      continue;
×
626
    }
627
    STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
29,456✔
628
    STREAM_CHECK_RET_GOTO(qStreamSetTableList(&sStreamReaderInfo->tableList, info->uid, info->groupId));
29,456✔
629
  }
630

631
end:
105,075✔
632
  taosWUnLockLatch(&sStreamReaderInfo->lock);
105,075✔
633
  return code;
104,856✔
634
}
635

636
static int32_t processTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* uidList, SArray** tableList) {
104,856✔
637
  int32_t code = 0;
104,856✔
638
  int32_t lino = 0;
104,856✔
639
  SNodeList* groupNew = NULL;   
104,856✔
640

641
  if (taosArrayGetSize(uidList) == 0) {
104,856✔
642
    return 0;
67,924✔
643
  }
644
  STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));  
36,932✔
645
  STREAM_CHECK_RET_GOTO(qStreamFilterTableListForReader(sStreamReaderInfo->pVnode, uidList, groupNew, sStreamReaderInfo->pTagCond,
36,932✔
646
                                                    sStreamReaderInfo->pTagIndexCond, &sStreamReaderInfo->storageApi,
647
                                                    sStreamReaderInfo->groupIdMap, sStreamReaderInfo->suid, tableList));
648

649
end:
36,713✔
650
  nodesDestroyList(groupNew);
36,932✔
651
  return code;
36,713✔
652
}
653

654
static int32_t scanCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
78,404✔
655
                             int64_t ver) {
656
  int32_t  code = 0;
78,404✔
657
  int32_t  lino = 0;
78,404✔
658
  SDecoder decoder = {0};
78,404✔
659
  SArray*  uidList = NULL;
78,404✔
660
  SArray*  tableList = NULL;
78,404✔
661
  SArray*  uidListAdd = NULL;
78,404✔
662
  void* pTask = sStreamReaderInfo->pTask;
78,404✔
663

664
  SVCreateTbBatchReq req = {0};
78,404✔
665
  tDecoderInit(&decoder, data, len);
78,404✔
666
  
667
  STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbBatchReq(&decoder, &req));
78,404✔
668

669
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
78,404✔
670
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
78,404✔
671

672
  if (sStreamReaderInfo->isVtableStream) {
78,404✔
673
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
5,572✔
674
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
5,572✔
675
  }
676
  
677
  SVCreateTbReq* pCreateReq = NULL;
78,404✔
678
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
156,808✔
679
    pCreateReq = req.pReqs + iReq;
78,180✔
680
    if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
78,180✔
681
      ST_TASK_DLOG("stream reader scan create table jump, %s", pCreateReq->name);
67,700✔
682
      continue;
67,924✔
683
    }
684
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
10,480✔
685
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
20,960✔
686
  }
687
  
688
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
78,628✔
689
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
78,404✔
690
  if (sStreamReaderInfo->isVtableStream) {
78,404✔
691
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
5,572✔
692
  }
693

694
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
78,404✔
695
end:
78,404✔
696
  taosArrayDestroy(uidList);
78,404✔
697
  taosArrayDestroy(uidListAdd);
78,404✔
698
  taosArrayDestroy(tableList);
78,404✔
699
  tDeleteSVCreateTbBatchReq(&req);
78,404✔
700
  tDecoderClear(&decoder);
78,404✔
701
  return code;
78,404✔
702
}
703

704
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq, int64_t ver) {
6,174,634✔
705
  int32_t  code = 0;
6,174,634✔
706
  int32_t  lino = 0;
6,174,634✔
707
  void*    pTask = sStreamReaderInfo->pTask;
6,174,634✔
708
  SArray*  uidList = NULL;
6,176,488✔
709
  SArray*  tableList = NULL;
6,176,488✔
710

711
  ST_TASK_DLOG("%s start, name:%s uid:%"PRId64, __func__, pCreateReq->name, pCreateReq->uid);
6,176,435✔
712
  if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false) ||
6,178,889✔
713
      ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
2,262✔
714
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
6,175,012✔
715
    goto end;
6,174,740✔
716
  }
717
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
1,648✔
718
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
1,648✔
719
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
3,296✔
720
  ST_TASK_DLOG("stream reader scan auto create table %s", pCreateReq->name);
1,648✔
721

722
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
1,648✔
723
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
1,648✔
724
end:
6,173,474✔
725
  taosArrayDestroy(uidList);
6,176,660✔
726
  taosArrayDestroy(tableList);
6,176,660✔
727
  return code;
6,176,660✔
728
}
729

730
static bool isColIdInList(SNodeList* colList, col_id_t cid){
544✔
731
  int32_t  code = 0;
544✔
732
  int32_t  lino = 0;
544✔
733
  SNode*  nodeItem = NULL;
544✔
734
  FOREACH(nodeItem, colList) {
1,360✔
735
    SNode*           pNode = ((STargetNode*)nodeItem)->pExpr;
1,360✔
736
    if (nodeType(pNode) == QUERY_NODE_COLUMN) {
1,360✔
737
      SColumnNode*     valueNode = (SColumnNode*)(pNode);
1,360✔
738
      if (cid == valueNode->colId) {
1,360✔
739
        return true;
544✔
740
      }
741
    }
742
  }
743
end:
×
744
  return false;
×
745
}
746

747
static bool isAlteredTable(int8_t action, ETableType tbType) {
25,348✔
748
  if (action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && tbType == TSDB_CHILD_TABLE) {
25,348✔
749
    return true;
24,804✔
750
  } else if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && tbType == TSDB_SUPER_TABLE) {
544✔
751
    return true;
×
752
  } else if ((action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) && 
544✔
753
     (tbType == TSDB_VIRTUAL_CHILD_TABLE || tbType == TSDB_VIRTUAL_NORMAL_TABLE)) {
544✔
754
    return true;
544✔
755
  }
756
  return false;
×
757
}
758

759
void getAlterColId(void* pVnode, int64_t uid, const char* colName, col_id_t* colId) {
544✔
760
  SSchemaWrapper *pSchema = metaGetTableSchema(((SVnode *)pVnode)->pMeta, uid, -1, 1, NULL, 0, false);
544✔
761
  if (pSchema == NULL) {
544✔
762
    return;
×
763
  }
764
  for (int32_t i = 0; i < pSchema->nCols; i++) {
1,360✔
765
    if (strncmp(pSchema->pSchema[i].name, colName, TSDB_COL_NAME_LEN) == 0) {
1,360✔
766
      *colId = pSchema->pSchema[i].colId;
544✔
767
      break;
544✔
768
    }
769
  }
770
  tDeleteSchemaWrapper(pSchema);
771
  return;
544✔
772
}
773

774
// Handle TSDB_ALTER_TABLE_ALTER_COLUMN_REF and TSDB_ALTER_TABLE_REMOVE_COLUMN_REF
775
static int32_t scanAlterTableColumnRef(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
544✔
776
                                       SVAlterTbReq* pReq, uint64_t uid, int64_t ver) {
777
  int32_t code = 0;
544✔
778
  int32_t lino = 0;
544✔
779
  void* pTask = sStreamReaderInfo->pTask;
544✔
780
  SArray* uidListAdd = NULL;
544✔
781

782
  uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
544✔
783
  STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
544✔
784

785
  uint64_t id = 0;
544✔
786
  STREAM_CHECK_CONDITION_GOTO(!uidInTableListOrigin(sStreamReaderInfo, sStreamReaderInfo->suid, uid, &id), TDB_CODE_SUCCESS);
544✔
787

788
  col_id_t colId = 0;
544✔
789
  getAlterColId(sStreamReaderInfo->pVnode, uid, pReq->colName, &colId);
544✔
790
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 0 && !isColIdInList(sStreamReaderInfo->triggerCols, colId)) {
544✔
791
    ST_TASK_ILOG("stream reader scan alter table %s, colId %d not in trigger cols", pReq->tbName, colId);
×
792
    goto end;
×
793
  }
794

795
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &uid), terrno);
544✔
796
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
544✔
797

798
  ST_TASK_DLOG("stream reader scan alter table column ref %s", pReq->tbName);
544✔
799

800
end:
544✔
801
  taosArrayDestroy(uidListAdd);
544✔
802
  STREAM_PRINT_LOG_END_WITHID(code, lino);
544✔
803
  return code;
544✔
804
}
805

806
static int32_t checkAlter(SStreamTriggerReaderInfo* sStreamReaderInfo, char* tbName, int8_t action, uint64_t *uid) {
25,348✔
807
  int32_t  code = 0;
25,348✔
808
  int32_t  lino = 0;
25,348✔
809
  ETableType tbType = 0;
25,348✔
810
  uint64_t suid = 0;
25,348✔
811

812
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, tbName, &tbType, &suid));
25,348✔
813
  STREAM_CHECK_CONDITION_GOTO(!isAlteredTable(action, tbType), TDB_CODE_SUCCESS);
25,348✔
814
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
25,348✔
815
  if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
23,654✔
816
    *uid = suid;
×
817
    goto end;
×
818
  }
819
  STREAM_CHECK_RET_GOTO(metaGetTableUidByName(sStreamReaderInfo->pVnode, tbName, uid));
23,654✔
820

821
end:
25,348✔
822
  return code;
25,348✔
823
}
824

825
static SArray* getTableListForAlterSuperTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SVAlterTbReq* pReq){
24,804✔
826
  int32_t code = 0;
24,804✔
827
  int32_t lino = 0;
24,804✔
828
  void* pTask = sStreamReaderInfo->pTask;
24,804✔
829
  SArray* uidList = taosArrayInit(8, sizeof(tb_uid_t));
24,804✔
830
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
24,804✔
831
  for (int32_t i = 0; i < taosArrayGetSize(pReq->tables); i++) {
49,608✔
832
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
24,804✔
833
    uint64_t uid = 0;
24,804✔
834
    code = checkAlter(sStreamReaderInfo, pTable->tbName, pReq->action, &uid);
24,804✔
835
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
24,804✔
836
      code = 0;
×
837
      ST_TASK_WLOG("stream reader scan alter table %s not exist, metaGetTableUidByName", pTable->tbName);
×
838
      continue;
×
839
    }
840
    STREAM_CHECK_RET_GOTO(code);
24,804✔
841
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, (const void *)&uid), terrno);
24,804✔
842
  }
843

844
end:
24,804✔
845
  if (code != 0) {
24,804✔
846
    ST_TASK_ELOG("%s failed,code:%d", __func__, code);
×
847
    taosArrayDestroy(uidList);
×
848
    uidList = NULL;
×
849
  }
850
  return uidList;
24,804✔
851
}
852

853
// Handle TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL and TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL
854
static int32_t scanAlterTableTagVal(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
24,804✔
855
                                    SArray* uidList, int64_t ver) {
856
  int32_t code = 0;
24,804✔
857
  int32_t lino = 0;
24,804✔
858
  void* pTask = sStreamReaderInfo->pTask;
24,804✔
859
  SArray* uidListAdd = NULL;
24,804✔
860
  SArray* uidListDel = NULL;
24,804✔
861
  SArray* tableList = NULL;
24,804✔
862

863
  if (sStreamReaderInfo->isVtableStream) {
24,804✔
864
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
1,452✔
865
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
1,452✔
866
  }
867

868
  uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
24,804✔
869
  STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
24,804✔
870

871
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
24,804✔
872
  STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
24,585✔
873

874
  if (rsp->checkAlter && taosArrayGetSize(uidListDel) > 0 && rsp->totalDataRows > 0) {
24,804✔
875
    rsp->needReturn = true;
×
876
    rsp->ver--;
×
877
    ST_TASK_DLOG("%s stream reader scan alter table need return data", __func__);
×
878
    goto end;
×
879
  }
880

881
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
24,804✔
882
  if (sStreamReaderInfo->isVtableStream) {
24,585✔
883
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
1,452✔
884
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
1,452✔
885
  }
886
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
24,585✔
887

888
  ST_TASK_DLOG("%s stream reader scan alter table tag val", __func__);
24,804✔
889

890
end:
24,804✔
891
  taosArrayDestroy(uidListAdd);
24,804✔
892
  taosArrayDestroy(uidListDel);
24,804✔
893
  taosArrayDestroy(tableList);
24,804✔
894
  STREAM_PRINT_LOG_END_WITHID(code, lino);
24,804✔
895
  return code;
24,804✔
896
}
897

898
static int32_t scanAlterTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
42,211✔
899
  int32_t  code = 0;
42,211✔
900
  int32_t  lino = 0;
42,211✔
901
  SDecoder decoder = {0};
42,211✔
902
  void* pTask = sStreamReaderInfo->pTask;
42,211✔
903
  SArray* uidList = NULL;
42,211✔
904

905
  ST_TASK_DLOG("%s start", __func__);
42,211✔
906

907
  SVAlterTbReq req = {0};
42,211✔
908
  tDecoderInit(&decoder, data, len);
42,211✔
909
  
910
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
42,211✔
911

912
  STREAM_CHECK_CONDITION_GOTO(req.action != TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && req.action != TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && 
42,211✔
913
    req.action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF && req.action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF, TDB_CODE_SUCCESS);
914

915
  uint64_t uid = 0;
30,745✔
916
  if (req.action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || req.action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
30,745✔
917
    STREAM_CHECK_CONDITION_GOTO(!sStreamReaderInfo->isVtableStream, TDB_CODE_SUCCESS);
5,941✔
918
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
544✔
919
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
544✔
920
      ST_TASK_WLOG("stream reader scan alter table %s not exist, metaGetTableUidByName", req.tbName);
×
921
      code = 0;
×
922
      goto end;
×
923
    }
924
    STREAM_CHECK_RET_GOTO(scanAlterTableColumnRef(sStreamReaderInfo, rsp, &req, uid, ver));
544✔
925
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
24,804✔
926
    uidList = getTableListForAlterSuperTable(sStreamReaderInfo, &req);
24,585✔
927
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
24,804✔
928
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
24,804✔
929
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
219✔
930
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
×
931
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
×
932
      ST_TASK_WLOG("stream reader scan alter table %s not exist, metaGetTableUidByName", req.tbName);
×
933
      code = 0;
×
934
      goto end;
×
935
    }
936
    uidList = taosArrayInit(8, sizeof(uint64_t));
×
937
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
×
938
    STREAM_CHECK_RET_GOTO(vnodeGetCtbIdList(sStreamReaderInfo->pVnode, uid, uidList));
×
939
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
×
940
  }
941

942
  ST_TASK_DLOG("%s stream reader scan alter table", __func__);
25,567✔
943

944
end:
42,430✔
945
  destroyAlterTbReq(&req);
42,211✔
946

947
  taosArrayDestroy(uidList);
42,211✔
948
  tDecoderClear(&decoder);
42,211✔
949
  STREAM_PRINT_LOG_END_WITHID(code, lino);
42,211✔
950
  return code;
42,211✔
951
}
952

953
// static int32_t scanAlterSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
954
//   int32_t  code = 0;
955
//   int32_t  lino = 0;
956
//   SDecoder decoder = {0};
957
//   SMAlterStbReq reqAlter = {0};
958
//   SVCreateStbReq req = {0};
959
//   tDecoderInit(&decoder, data, len);
960
//   void* pTask = sStreamReaderInfo->pTask;
961
  
962
//   STREAM_CHECK_RET_GOTO(tDecodeSVCreateStbReq(&decoder, &req));
963
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
964
//   if (req.alterOriData != 0) {
965
//     STREAM_CHECK_RET_GOTO(tDeserializeSMAlterStbReq(req.alterOriData, req.alterOriDataLen, &reqAlter));
966
//     STREAM_CHECK_CONDITION_GOTO(reqAlter.alterType != TSDB_ALTER_TABLE_DROP_TAG && reqAlter.alterType != TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TDB_CODE_SUCCESS);
967
//   }
968
  
969
//   STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo));
970

971
//   ST_TASK_DLOG("stream reader scan alter suid %" PRId64, req.suid);
972
// end:
973
//   tFreeSMAltertbReq(&reqAlter);
974
//   tDecoderClear(&decoder);
975
//   return code;
976
// }
977

978
// static int32_t scanDropSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
979
//   int32_t  code = 0;
980
//   int32_t  lino = 0;
981
//   SDecoder decoder = {0};
982
//   void* pTask = sStreamReaderInfo->pTask;
983

984
//   SVDropStbReq req = {0};
985
//   tDecoderInit(&decoder, data, len);
986
//   STREAM_CHECK_RET_GOTO(tDecodeSVDropStbReq(&decoder, &req));
987
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
988

989
//   ST_TASK_DLOG("stream reader scan drop suid %" PRId64, req.suid);
990
// end:
991
//   tDecoderClear(&decoder);
992
//   return code;
993
// }
994

995
static int32_t scanSubmitTbDataForMeta(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash, int64_t ver) {
45,718,187✔
996
  int32_t code = 0;
45,718,187✔
997
  int32_t lino = 0;
45,718,187✔
998
  WalMetaResult walMeta = {0};
45,718,187✔
999
  SSubmitTbData submitTbData = {0};
45,719,006✔
1000
  
1001
  if (tStartDecode(pCoder) < 0) {
45,719,229✔
1002
    code = TSDB_CODE_INVALID_MSG;
×
1003
    TSDB_CHECK_CODE(code, lino, end);
×
1004
  }
1005

1006
  uint8_t       version = 0;
45,721,166✔
1007
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
45,718,036✔
1008
    code = TSDB_CODE_INVALID_MSG;
×
1009
    TSDB_CHECK_CODE(code, lino, end);
×
1010
  }
1011
  version = (submitTbData.flags >> 8) & 0xff;
45,718,036✔
1012
  submitTbData.flags = submitTbData.flags & 0xff;
45,718,036✔
1013

1014
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1015
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
45,718,036✔
1016
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
5,060,941✔
1017
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
5,061,296✔
1018
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
5,061,296✔
1019
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
5,060,995✔
1020
  }
1021

1022
  // submit data
1023
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
45,716,139✔
1024
    code = TSDB_CODE_INVALID_MSG;
×
1025
    TSDB_CHECK_CODE(code, lino, end);
×
1026
  }
1027
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
45,717,860✔
1028
    code = TSDB_CODE_INVALID_MSG;
×
1029
    TSDB_CHECK_CODE(code, lino, end);
×
1030
  }
1031

1032
  if (!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &walMeta.id, false)){
45,717,860✔
1033
    goto end;
36,756,681✔
1034
  }
1035
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
8,965,840✔
1036
    code = TSDB_CODE_INVALID_MSG;
×
1037
    TSDB_CHECK_CODE(code, lino, end);
×
1038
  }
1039

1040
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
8,965,840✔
1041
    uint64_t nColData = 0;
×
1042
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1043
      code = TSDB_CODE_INVALID_MSG;
×
1044
      TSDB_CHECK_CODE(code, lino, end);
×
1045
    }
1046

1047
    SColData colData = {0};
×
1048
    code = tDecodeColData(version, pCoder, &colData, false);
×
1049
    if (code) {
×
1050
      code = TSDB_CODE_INVALID_MSG;
×
1051
      TSDB_CHECK_CODE(code, lino, end);
×
1052
    }
1053

1054
    if (colData.flag != HAS_VALUE) {
×
1055
      code = TSDB_CODE_INVALID_MSG;
×
1056
      TSDB_CHECK_CODE(code, lino, end);
×
1057
    }
1058
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
1059
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
1060

1061
    for (uint64_t i = 1; i < nColData; i++) {
×
1062
      code = tDecodeColData(version, pCoder, &colData, true);
×
1063
      if (code) {
×
1064
        code = TSDB_CODE_INVALID_MSG;
×
1065
        TSDB_CHECK_CODE(code, lino, end);
×
1066
      }
1067
    }
1068
  } else {
1069
    uint64_t nRow = 0;
8,965,840✔
1070
    if (tDecodeU64v(pCoder, &nRow) < 0) {
8,965,584✔
1071
      code = TSDB_CODE_INVALID_MSG;
×
1072
      TSDB_CHECK_CODE(code, lino, end);
×
1073
    }
1074

1075
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
19,280,889✔
1076
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
10,315,268✔
1077
      pCoder->pos += pRow->len;
10,314,449✔
1078
      if (iRow == 0){
10,314,540✔
1079
#ifndef NO_UNALIGNED_ACCESS
1080
        walMeta.skey = pRow->ts;
8,965,621✔
1081
#else
1082
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1083
#endif
1084
      }
1085
      if (iRow == nRow - 1) {
10,314,759✔
1086
#ifndef NO_UNALIGNED_ACCESS
1087
        walMeta.ekey = pRow->ts;
8,965,840✔
1088
#else
1089
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
1090
#endif
1091
      }
1092
    }
1093
  }
1094

1095
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
8,965,621✔
1096
  if (data != NULL) {
8,965,149✔
1097
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
1,092✔
1098
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
1,092✔
1099
  } else {
1100
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
8,964,057✔
1101
  }
1102

1103
end:
45,715,534✔
1104
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
45,717,380✔
1105
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
45,714,181✔
1106
  tEndDecode(pCoder);
45,714,435✔
1107
  return code;
45,717,249✔
1108
}
1109

1110
static int32_t scanSubmitDataForMeta(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
45,721,494✔
1111
  int32_t  code = 0;
45,721,494✔
1112
  int32_t  lino = 0;
45,721,494✔
1113
  SDecoder decoder = {0};
45,721,494✔
1114
  SSHashObj* gidHash = NULL;
45,721,494✔
1115
  void* pTask = sStreamReaderInfo->pTask;
45,721,494✔
1116

1117
  tDecoderInit(&decoder, data, len);
45,722,341✔
1118
  if (tStartDecode(&decoder) < 0) {
45,719,587✔
1119
    code = TSDB_CODE_INVALID_MSG;
×
1120
    TSDB_CHECK_CODE(code, lino, end);
×
1121
  }
1122

1123
  uint64_t nSubmitTbData = 0;
45,714,137✔
1124
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
45,719,269✔
1125
    code = TSDB_CODE_INVALID_MSG;
×
1126
    TSDB_CHECK_CODE(code, lino, end);
×
1127
  }
1128

1129
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
45,719,269✔
1130
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
45,710,590✔
1131

1132
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
91,426,372✔
1133
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash, ver));
45,712,802✔
1134
  }
1135
  tEndDecode(&decoder);
45,713,570✔
1136

1137
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
45,709,706✔
1138
  int32_t iter = 0;
45,711,192✔
1139
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
45,711,192✔
1140
  while (px != NULL) {
54,662,656✔
1141
    WalMetaResult* pMeta = (WalMetaResult*)px;
8,955,471✔
1142
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
8,955,471✔
1143
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
8,964,279✔
1144
    rsp->totalRows++;
8,964,279✔
1145
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
8,964,279✔
1146
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1147
    px = tSimpleHashIterate(gidHash, px, &iter);
8,964,357✔
1148
  }
1149
end:
45,707,185✔
1150
  tDecoderClear(&decoder);
45,709,998✔
1151
  tSimpleHashCleanup( gidHash);
45,721,752✔
1152
  return code;
45,715,931✔
1153
}
1154

1155
static int32_t createBlockForTsdbMeta(SSDataBlock** pBlock, bool isVTable) {
531,671✔
1156
  int32_t code = 0;
531,671✔
1157
  int32_t lino = 0;
531,671✔
1158
  SArray* schemas = taosArrayInit(8, sizeof(SSchema));
531,671✔
1159
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
531,479✔
1160

1161
  int32_t index = 1;
531,479✔
1162
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // skey
531,479✔
1163
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // ekey
531,671✔
1164
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // uid
531,671✔
1165
  if (!isVTable) {
531,671✔
1166
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_UBIGINT, LONG_BYTES, index++))  // gid
68,126✔
1167
  }
1168
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))     // nrows
531,671✔
1169

1170
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
531,671✔
1171

1172
end:
531,671✔
1173
  taosArrayDestroy(schemas);
531,671✔
1174
  return code;
531,671✔
1175
}
1176

1177
static int32_t createBlockForWalMetaNew(SSDataBlock** pBlock) {
384,298✔
1178
  int32_t code = 0;
384,298✔
1179
  int32_t lino = 0;
384,298✔
1180
  SArray* schemas = NULL;
384,298✔
1181

1182
  schemas = taosArrayInit(8, sizeof(SSchema));
384,298✔
1183
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
384,298✔
1184

1185
  int32_t index = 0;
384,298✔
1186
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
384,298✔
1187
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // skey
384,106✔
1188
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ekey
384,298✔
1189
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
384,298✔
1190

1191
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
384,298✔
1192

1193
end:
384,298✔
1194
  taosArrayDestroy(schemas);
384,298✔
1195
  return code;
384,298✔
1196
}
1197

1198
static int32_t processMeta(int16_t msgType, SStreamTriggerReaderInfo* sStreamReaderInfo, void *data, int32_t len, SSTriggerWalNewRsp* rsp, int64_t ver) {
653,790✔
1199
  int32_t code = 0;
653,790✔
1200
  int32_t lino = 0;
653,790✔
1201
  void* pTask = sStreamReaderInfo->pTask;
653,790✔
1202

1203
  ST_TASK_DLOG("%s check meta msg, stream ver:%" PRId64 ", wal ver:%" PRId64, __func__, sStreamReaderInfo->tableList.version, ver);
653,790✔
1204

1205
  SDecoder dcoder = {0};
653,790✔
1206
  tDecoderInit(&dcoder, data, len);
653,790✔
1207
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
653,790✔
1208
    if (rsp->deleteBlock == NULL) {
34,740✔
1209
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock));
12,308✔
1210
    }
1211
      
1212
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
34,740✔
1213
  } else if (msgType == TDMT_VND_DROP_TABLE && 
619,050✔
1214
    (sStreamReaderInfo->deleteOutTbl != 0 || sStreamReaderInfo->isVtableStream)) {
58,036✔
1215
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
37,246✔
1216
  // } else if (msgType == TDMT_VND_DROP_STB) {
1217
  //   STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
1218
  } else if (msgType == TDMT_VND_CREATE_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
581,804✔
1219
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, rsp, data, len, ver));
78,404✔
1220
  } else if (msgType == TDMT_VND_ALTER_STB && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
503,400✔
1221
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
1222
  } else if (msgType == TDMT_VND_ALTER_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
474,294✔
1223
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, rsp, data, len, ver));
42,211✔
1224
  }
1225

1226
end:
653,571✔
1227
  tDecoderClear(&dcoder);
653,571✔
1228
  return code;
653,571✔
1229
}
1230
static int32_t processWalVerMetaNew(SVnode* pVnode, SSTriggerWalNewRsp* rsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
16,869,251✔
1231
                       int64_t ctime) {
1232
  int32_t code = 0;
16,869,251✔
1233
  int32_t lino = 0;
16,869,251✔
1234
  void* pTask = sStreamReaderInfo->pTask;
16,869,251✔
1235

1236
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
16,868,978✔
1237
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
16,869,524✔
1238
  code = walReaderSeekVer(pWalReader, rsp->ver);
16,869,524✔
1239
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
16,867,215✔
1240
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
9,430,983✔
1241
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
1242
      rsp->verTime = 0;
×
1243
    } else {
1244
      rsp->verTime = taosGetTimestampUs();
9,430,936✔
1245
    }
1246
    ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
9,430,711✔
1247
    code = TSDB_CODE_SUCCESS;
9,432,582✔
1248
    goto end;
9,432,582✔
1249
  }
1250
  STREAM_CHECK_RET_GOTO(code);
7,436,232✔
1251

1252
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
7,436,232✔
1253
  while (1) {
46,142,968✔
1254
    code = walNextValidMsg(pWalReader, true);
53,579,110✔
1255
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
53,579,750✔
1256
      rsp->verTime = taosGetTimestampUs();
7,433,667✔
1257
      ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
7,433,667✔
1258
      code = TSDB_CODE_SUCCESS;
7,434,514✔
1259
      goto end;
7,434,514✔
1260
    }
1261
    STREAM_CHECK_RET_GOTO(code);
46,145,528✔
1262
    rsp->ver = pWalReader->curVersion;
46,145,528✔
1263
    SWalCont* wCont = &pWalReader->pHead->head;
46,149,656✔
1264
    rsp->verTime = wCont->ingestTs;
46,149,921✔
1265
    if (wCont->ingestTs / 1000 > ctime) break;
46,149,028✔
1266
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
46,148,578✔
1267
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
46,146,077✔
1268
    int64_t ver = wCont->version;
46,147,128✔
1269

1270
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 "/%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d",
46,147,474✔
1271
      TD_VID(pVnode), ver, walGetAppliedVer(pWalReader->pWal), TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
1272
    if (wCont->msgType == TDMT_VND_SUBMIT) {
46,153,138✔
1273
      // return when getting data if there are meta data in vtable scan
1274
      if (sStreamReaderInfo->isVtableStream && rsp->tableBlock != NULL && ((SSDataBlock*)rsp->tableBlock)->info.rows > 0) {
45,723,891✔
1275
        rsp->ver--;
2,482✔
1276
        break;
2,482✔
1277
      }
1278
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
45,720,863✔
1279
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
45,722,521✔
1280
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver));
45,722,248✔
1281
    } else {
1282
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
428,157✔
1283
    }
1284

1285
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
46,144,278✔
1286
      break;
×
1287
    }
1288
  }
1289

1290
end:
16,869,578✔
1291
  walCloseReader(pWalReader);
16,869,578✔
1292
  return code;
16,868,677✔
1293
}
1294

1295
int32_t cacheTag(SVnode* pVnode, SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr, SStorageAPI* api, uint64_t uid, col_id_t colId, SRWLatch* lock) {
46,096,882✔
1296
  int32_t     code = 0;
46,096,882✔
1297
  int32_t     lino = 0;
46,096,882✔
1298
  SMetaReader mr = {0};
46,096,882✔
1299
  SArray* tagCache = NULL;
46,104,185✔
1300
  char* data = NULL;
46,109,245✔
1301

1302
  if (lock != NULL) taosWLockLatch(lock);
46,111,991✔
1303
  STREAM_CHECK_CONDITION_GOTO(numOfExpr == 0, code);
46,131,735✔
1304
  stDebug("%s start,uid:%"PRIu64, __func__, uid);
2,654,450✔
1305
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
2,655,091✔
1306
  if (uidData == NULL) {
2,654,669✔
1307
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
2,630,676✔
1308
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
2,630,676✔
1309
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
2,630,676✔
1310
      taosArrayDestroy(tagCache);
×
1311
      code = terrno;
×
1312
      goto end;
×
1313
    }
1314
  } else {
1315
    tagCache = *(SArray**)uidData;
23,993✔
1316
    stDebug("%s found tagCache, size:%zu %d, uid:%"PRIu64, __func__, taosArrayGetSize(tagCache), numOfExpr, uid);
23,993✔
1317
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
23,993✔
1318
  }
1319
  
1320
  api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn);
2,654,669✔
1321
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
2,654,669✔
1322
  api->metaReaderFn.readerReleaseLock(&mr);
2,649,605✔
1323
  STREAM_CHECK_RET_GOTO(code);
2,652,559✔
1324
  
1325
  for (int32_t j = 0; j < numOfExpr; ++j) {
9,124,403✔
1326
    const SExprInfo* pExpr1 = &pExprInfo[j];
6,478,787✔
1327
    int32_t functionId = pExpr1->pExpr->_function.functionId;
6,481,741✔
1328
    col_id_t cid = 0;
6,488,875✔
1329
    // this is to handle the tbname
1330
    if (fmIsScanPseudoColumnFunc(functionId)) {
6,488,875✔
1331
      int32_t fType = pExpr1->pExpr->_function.functionType;
777,659✔
1332
      if (fType == FUNCTION_TYPE_TBNAME) {
777,659✔
1333
        data = taosMemoryCalloc(1, strlen(mr.me.name) + VARSTR_HEADER_SIZE);
777,659✔
1334
        STREAM_CHECK_NULL_GOTO(data, terrno);
777,659✔
1335
        STR_TO_VARSTR(data, mr.me.name)
777,659✔
1336
      }
1337
      cid = -1;
777,659✔
1338
    } else {  // these are tags
1339
      const char* p = NULL;
5,706,574✔
1340
      char* pData = NULL;
5,706,574✔
1341
      int8_t type = pExpr1->base.resSchema.type;
5,706,574✔
1342
      int32_t len = pExpr1->base.resSchema.bytes;
5,705,750✔
1343
      STagVal tagVal = {0};
5,710,372✔
1344
      tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
5,711,216✔
1345
      cid = tagVal.cid;
5,711,064✔
1346
      if (colId != 0 && cid != colId) {
5,711,064✔
1347
        continue;
1,511✔
1348
      }
1349
      p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, type, &tagVal);
5,709,553✔
1350

1351
      if (type != TSDB_DATA_TYPE_JSON && p != NULL) {
5,705,947✔
1352
        pData = tTagValToData((const STagVal*)p, false);
5,702,662✔
1353
      } else {
1354
        pData = (char*)p;
3,285✔
1355
      }
1356

1357
      if (pData != NULL && (type == TSDB_DATA_TYPE_JSON || !IS_VAR_DATA_TYPE(type))) {
5,703,013✔
1358
        if (type == TSDB_DATA_TYPE_JSON) {
2,856,884✔
1359
          len = getJsonValueLen(pData);
×
1360
        }
1361
        data = taosMemoryCalloc(1, len);
2,856,884✔
1362
        STREAM_CHECK_NULL_GOTO(data, terrno);
2,862,310✔
1363
        (void)memcpy(data, pData, len);
2,862,310✔
1364
      } else {
1365
        data = pData;
2,846,129✔
1366
      }
1367
    }
1368
    if (uidData == NULL){
6,484,430✔
1369
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
12,875,301✔
1370
    } else {
1371
      void* pre = taosArrayGetP(tagCache, j);
45,991✔
1372
      taosMemoryFree(pre);
45,991✔
1373
      taosArraySet(tagCache, j, &data);
45,991✔
1374
    }
1375
    data = NULL;
6,477,387✔
1376
  }
1377

1378
end:
46,130,635✔
1379
  taosMemoryFree(data);
46,125,140✔
1380
  api->metaReaderFn.clearReader(&mr);
46,115,852✔
1381
  if (lock != NULL) taosWUnLockLatch(lock);
46,099,975✔
1382
  return code;
46,126,844✔
1383
}
1384

1385
int32_t fillTag(SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr,
459,167,849✔
1386
                uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks, SRWLatch* lock) {
1387
  int32_t     code = 0;
459,167,849✔
1388
  int32_t     lino = 0;
459,167,849✔
1389
  SArray* tagCache = NULL;
459,167,849✔
1390
  if (numOfExpr == 0) {
459,167,849✔
1391
    return TSDB_CODE_SUCCESS;
42,807,326✔
1392
  }
1393

1394
  taosRLockLatch(lock);
416,360,523✔
1395
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
416,480,796✔
1396
  if (uidData == NULL) {
416,404,342✔
1397
    stError("%s error uidData is null,uid:%"PRIu64, __func__, uid);
×
1398
  } else {
1399
    tagCache = *(SArray**)uidData;
416,404,342✔
1400
    if(taosArrayGetSize(tagCache) != numOfExpr) {
416,424,989✔
1401
      stError("%s numOfExpr:%d,tagCache size:%zu", __func__, numOfExpr, taosArrayGetSize(tagCache));
×
1402
      tagCache = NULL;
×
1403
    }
1404
  }
1405
  
1406
  for (int32_t j = 0; j < numOfExpr; ++j) {
1,429,651,495✔
1407
    const SExprInfo* pExpr1 = &pExprInfo[j];
1,013,176,017✔
1408
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
1,013,078,354✔
1409

1410
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
1,013,110,572✔
1411
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
1,012,052,748✔
1412
    int32_t functionId = pExpr1->pExpr->_function.functionId;
1,012,052,748✔
1413

1414
    // this is to handle the tbname
1415
    if (fmIsScanPseudoColumnFunc(functionId)) {
1,012,285,612✔
1416
      int32_t fType = pExpr1->pExpr->_function.functionType;
4,323,062✔
1417
      if (fType == FUNCTION_TYPE_TBNAME) {
4,323,062✔
1418
        pColInfoData->info.colId = -1;
4,323,062✔
1419
      }
1420
    } 
1421
    char* data = tagCache == NULL ? NULL : taosArrayGetP(tagCache, j);
1,012,534,415✔
1422

1423
    bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
1,011,161,287✔
1424
    if (isNullVal) {
1,011,313,754✔
1425
      colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
1426
    } else {
1427
      if (!IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
1,011,313,754✔
1428
        for (uint32_t i = 0; i < numOfRows; i++){
2,147,483,647✔
1429
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
2,147,483,647✔
1430
        }
1431
      }
1432
      code = colDataSetNItems(pColInfoData, currentRow, (const char*)data, numOfRows, numOfBlocks, false);
1,012,035,323✔
1433
      STREAM_CHECK_RET_GOTO(code);
1,013,206,077✔
1434
    }
1435
  }
1436
end:
416,475,478✔
1437
  taosRUnLockLatch(lock);
416,475,829✔
1438
  return code;
416,476,689✔
1439
}
1440

1441
static int32_t processTag(SStreamTriggerReaderInfo* info, bool isCalc, 
4,194,661✔
1442
  uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks) {
1443
  int32_t     code = 0;
4,194,661✔
1444
  int32_t     lino = 0;
4,194,661✔
1445

1446
  void* pTask = info->pTask;
4,194,661✔
1447
  ST_TASK_DLOG("%s start. rows:%" PRIu32 ",uid:%"PRIu64, __func__,  numOfRows, uid);
4,195,497✔
1448
  
1449
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
4,196,333✔
1450
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
4,196,333✔
1451
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
4,196,060✔
1452
  
1453
  code = fillTag(metaCache, pExprInfo, numOfExpr, uid, pBlock, currentRow, numOfRows, numOfBlocks, &info->lock);
4,195,788✔
1454
  STREAM_CHECK_RET_GOTO(code);
4,197,424✔
1455

1456
end:
4,197,424✔
1457
  return code;
4,197,424✔
1458
}
1459

1460
int32_t getRowRange(SColData* pCol, STimeWindow* window, int32_t* rowStart, int32_t* rowEnd, int32_t* nRows) {
×
1461
  int32_t code = 0;
×
1462
  int32_t lino = 0;
×
1463
  *nRows = 0;
×
1464
  *rowStart = 0;
×
1465
  *rowEnd = pCol->nVal;
×
1466
  if (window != NULL) {
×
1467
    SColVal colVal = {0};
×
1468
    *rowStart = -1;
×
1469
    *rowEnd = -1;
×
1470
    for (int32_t k = 0; k < pCol->nVal; k++) {
×
1471
      STREAM_CHECK_RET_GOTO(tColDataGetValue(pCol, k, &colVal));
×
1472
      int64_t ts = VALUE_GET_TRIVIAL_DATUM(&colVal.value);
×
1473
      if (ts >= window->skey && *rowStart == -1) {
×
1474
        *rowStart = k;
×
1475
      }
1476
      if (ts > window->ekey && *rowEnd == -1) {
×
1477
        *rowEnd = k;
×
1478
      }
1479
    }
1480
    STREAM_CHECK_CONDITION_GOTO(*rowStart == -1 || *rowStart == *rowEnd, TDB_CODE_SUCCESS);
×
1481

1482
    if (*rowStart != -1 && *rowEnd == -1) {
×
1483
      *rowEnd = pCol->nVal;
×
1484
    }
1485
  }
1486
  *nRows = *rowEnd - *rowStart;
×
1487

1488
end:
×
1489
  return code;
×
1490
}
1491

1492
static int32_t setColData(int64_t rows, int32_t rowStart, int32_t rowEnd, SColData* colData, SColumnInfoData* pColData) {
×
1493
  int32_t code = 0;
×
1494
  int32_t lino = 0;
×
1495
  for (int32_t k = rowStart; k < rowEnd; k++) {
×
1496
    SColVal colVal = {0};
×
1497
    STREAM_CHECK_RET_GOTO(tColDataGetValue(colData, k, &colVal));
×
1498
    STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, rows + k - rowStart, VALUE_GET_DATUM(&colVal.value, colVal.value.type),
×
1499
                                        !COL_VAL_IS_VALUE(&colVal)));
1500
  }
1501
  end:
×
1502
  return code;
×
1503
}
1504

1505
static int32_t getColId(int64_t suid, int64_t uid, int16_t i, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, int16_t* colId) {
24,274,483✔
1506
  int32_t code = 0;
24,274,483✔
1507
  int32_t lino = 0;
24,274,483✔
1508
  int64_t id[2] = {suid, uid};
24,274,483✔
1509
  taosRLockLatch(&sStreamReaderInfo->lock);
24,274,483✔
1510
  void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
24,276,667✔
1511
  STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
24,272,845✔
1512
  SSHashObj* uInfo = *(SSHashObj **)px;
24,272,845✔
1513
  STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
24,273,118✔
1514
  int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
24,273,118✔
1515
  if (tmp != NULL) {
24,272,327✔
1516
    *colId = *tmp;
22,279,776✔
1517
  } else {
1518
    *colId = -1;
1,992,551✔
1519
  }
1520

1521
end:
24,275,876✔
1522
  taosRUnLockLatch(&sStreamReaderInfo->lock);
24,273,146✔
1523
  return code;
24,278,060✔
1524
}
1525

1526
static int32_t getSchemas(SVnode* pVnode, int64_t suid, int64_t uid, int32_t sver, SStreamTriggerReaderInfo* sStreamReaderInfo, STSchema** schema) {
11,697,142✔
1527
  int32_t code = 0;
11,697,142✔
1528
  int32_t lino = 0;
11,697,142✔
1529
  int64_t id = suid != 0 ? suid : uid;
11,697,142✔
1530
  if (sStreamReaderInfo->isVtableStream) {
11,696,869✔
1531
    STSchema** schemaTmp = taosHashGet(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES);
7,806,229✔
1532
    if (schemaTmp == NULL || *schemaTmp == NULL || (*schemaTmp)->version != sver) {
7,806,229✔
1533
      *schema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
72,144✔
1534
      STREAM_CHECK_NULL_GOTO(*schema, terrno);
72,417✔
1535
      code = taosHashPut(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES, schema, POINTER_BYTES);
72,417✔
1536
      if (code != 0) {
72,417✔
1537
        taosMemoryFree(*schema);
×
1538
        goto end;
×
1539
      }
1540
    } else {
1541
      *schema = *schemaTmp;
7,733,812✔
1542
    }
1543
  } else {
1544
    if (sStreamReaderInfo->triggerTableSchema == NULL || sStreamReaderInfo->triggerTableSchema->version != sver) {
3,890,628✔
1545
      taosMemoryFree(sStreamReaderInfo->triggerTableSchema);
112,251✔
1546
      sStreamReaderInfo->triggerTableSchema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
112,524✔
1547
      STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerTableSchema, terrno);
112,252✔
1548
    }
1549
    *schema = sStreamReaderInfo->triggerTableSchema;
3,891,446✔
1550
  }
1551
  
1552
end:
11,697,145✔
1553
  return code;
11,696,298✔
1554
}
1555

1556
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, 
12,930,743✔
1557
  SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
1558
  int32_t code = 0;
12,930,743✔
1559
  int32_t lino = 0;
12,930,743✔
1560
  uint64_t id = 0;
12,930,743✔
1561
  WalMetaResult walMeta = {0};
12,931,032✔
1562
  void* pTask = sStreamReaderInfo->pTask;
12,928,328✔
1563
  SSDataBlock * pBlock = (SSDataBlock*)rsp->dataBlock;
12,928,603✔
1564

1565
  if (tStartDecode(pCoder) < 0) {
12,928,059✔
1566
    ST_TASK_ELOG("vgId:%d %s invalid submit data", TD_VID(pVnode), __func__);
×
1567
    code = TSDB_CODE_INVALID_MSG;
×
1568
    TSDB_CHECK_CODE(code, lino, end);
×
1569
  }
1570

1571
  SSubmitTbData submitTbData = {0};
12,928,323✔
1572
  uint8_t       version = 0;
12,927,506✔
1573
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
12,926,967✔
1574
    ST_TASK_ELOG("vgId:%d %s invalid submit data flags", TD_VID(pVnode), __func__);
×
1575
    code = TSDB_CODE_INVALID_MSG;
×
1576
    TSDB_CHECK_CODE(code, lino, end);
×
1577
  }
1578
  version = (submitTbData.flags >> 8) & 0xff;
12,926,967✔
1579
  submitTbData.flags = submitTbData.flags & 0xff;
12,926,967✔
1580
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1581
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
12,926,967✔
1582
    if (tStartDecode(pCoder) < 0) {
270,882✔
1583
      ST_TASK_ELOG("vgId:%d %s invalid auto create table data", TD_VID(pVnode), __func__);
×
1584
      code = TSDB_CODE_INVALID_MSG;
×
1585
      TSDB_CHECK_CODE(code, lino, end);
×
1586
    }
1587
    tEndDecode(pCoder);
270,882✔
1588
  }
1589

1590
  // submit data
1591
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
12,925,845✔
1592
    ST_TASK_ELOG("vgId:%d %s invalid submit data suid", TD_VID(pVnode), __func__);
×
1593
    code = TSDB_CODE_INVALID_MSG;
×
1594
    TSDB_CHECK_CODE(code, lino, end);
×
1595
  }
1596
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
12,925,875✔
1597
    ST_TASK_ELOG("vgId:%d %s invalid submit data uid", TD_VID(pVnode), __func__);
×
1598
    code = TSDB_CODE_INVALID_MSG;
×
1599
    TSDB_CHECK_CODE(code, lino, end);
×
1600
  }
1601

1602
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64 ", ver:%" PRId64, __func__, submitTbData.uid, submitTbData.suid, ver);
12,925,875✔
1603

1604
  if (rsp->uidHash != NULL) {
12,925,840✔
1605
    uint64_t* gid = tSimpleHashGet(rsp->uidHash, &submitTbData.uid, LONG_BYTES);
9,589,953✔
1606
    STREAM_CHECK_CONDITION_GOTO(gid == NULL, TDB_CODE_SUCCESS);
9,588,590✔
1607
    ST_TASK_DLOG("%s get uid gid from uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%"PRIu64, __func__, submitTbData.uid, submitTbData.suid, *gid);
9,588,590✔
1608
    id = *gid;
9,588,863✔
1609
  } else {
1610
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &id, rsp->isCalc), TDB_CODE_SUCCESS);
3,336,995✔
1611
  }
1612

1613
  walMeta.id = id;
11,694,130✔
1614
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
11,694,130✔
1615

1616
  if (ranges != NULL){
11,696,037✔
1617
    void* timerange = tSimpleHashGet(ranges, &id, sizeof(id));
9,588,864✔
1618
    if (timerange == NULL) goto end;;
9,589,121✔
1619
    int64_t* pRange = (int64_t*)timerange;
9,589,121✔
1620
    window.skey = pRange[0];
9,589,121✔
1621
    window.ekey = pRange[1];
9,589,394✔
1622
    ST_TASK_DLOG("%s get time range from ranges, uid:%" PRId64 ", suid:%" PRId64 ", gid:%" PRIu64 ", skey:%" PRId64 ", ekey:%" PRId64,
9,589,121✔
1623
      __func__, submitTbData.uid, submitTbData.suid, id, window.skey, window.ekey);
1624
  }
1625
  
1626
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
11,697,674✔
1627
    ST_TASK_ELOG("vgId:%d %s invalid submit data sver", TD_VID(pVnode), __func__);
×
1628
    code = TSDB_CODE_INVALID_MSG;
×
1629
    TSDB_CHECK_CODE(code, lino, end);
×
1630
  }
1631

1632
  STSchema*    schema = NULL;
11,697,674✔
1633
  STREAM_CHECK_RET_GOTO(getSchemas(pVnode, submitTbData.suid, submitTbData.uid, submitTbData.sver, sStreamReaderInfo, &schema));
11,697,129✔
1634

1635
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, &submitTbData.uid, LONG_BYTES);
11,695,238✔
1636
  int32_t blockStart = 0;
11,695,186✔
1637
  int32_t numOfRows = 0;
11,695,186✔
1638
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,694,097✔
1639
    uint64_t nColData = 0;
×
1640
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1641
      ST_TASK_ELOG("vgId:%d %s invalid submit data nColData", TD_VID(pVnode), __func__);
×
1642
      code = TSDB_CODE_INVALID_MSG;
×
1643
      TSDB_CHECK_CODE(code, lino, end);
×
1644
    }
1645

1646
    SColData colData = {0};
×
1647
    code = tDecodeColData(version, pCoder, &colData, false);
×
1648
    if (code) {
×
1649
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData", TD_VID(pVnode), __func__);
×
1650
      code = TSDB_CODE_INVALID_MSG;
×
1651
      TSDB_CHECK_CODE(code, lino, end);
×
1652
    }
1653

1654
    if (colData.flag != HAS_VALUE) {
×
1655
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData flag", TD_VID(pVnode), __func__);
×
1656
      code = TSDB_CODE_INVALID_MSG;
×
1657
      TSDB_CHECK_CODE(code, lino, end);
×
1658
    }
1659
    
1660
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
1661
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
1662

1663
    int32_t rowStart = 0;
×
1664
    int32_t rowEnd = 0;
×
1665
    STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, &numOfRows));
×
1666
    STREAM_CHECK_CONDITION_GOTO(numOfRows <= 0, TDB_CODE_SUCCESS);
×
1667

1668
    STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
×
1669
    blockStart = pSlice->currentRowIdx;
×
1670
    int32_t pos = pCoder->pos;
×
1671
    for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
×
1672
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
×
1673
      STREAM_CHECK_NULL_GOTO(pColData, terrno);
×
1674
      if (pColData->info.colId <= -1) {
×
1675
        pColData->hasNull = true;
×
1676
        continue;
×
1677
      }
1678
      if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
×
1679
        STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colData, pColData));
×
1680
        continue;
×
1681
      }
1682

1683
      pCoder->pos = pos;
×
1684

1685
      int16_t colId = 0;
×
1686
      if (sStreamReaderInfo->isVtableStream){
×
1687
        STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
×
1688
        ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
×
1689
      } else {
1690
        colId = pColData->info.colId;
×
1691
      }
1692
      
1693
      uint64_t j = 1;
×
1694
      for (; j < nColData; j++) {
×
1695
        int16_t cid = 0;
×
1696
        int32_t posTmp = pCoder->pos;
×
1697
        pCoder->pos += INT_BYTES;
×
1698
        if ((code = tDecodeI16v(pCoder, &cid))) return code;
×
1699
        pCoder->pos = posTmp;
×
1700
        if (cid == colId) {
×
1701
          SColData colDataTmp = {0};
×
1702
          code = tDecodeColData(version, pCoder, &colDataTmp, false);
×
1703
          if (code) {
×
1704
            code = TSDB_CODE_INVALID_MSG;
×
1705
            TSDB_CHECK_CODE(code, lino, end);
×
1706
          }
1707
          STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colDataTmp, pColData));
×
1708
          break;
×
1709
        }
1710
        code = tDecodeColData(version, pCoder, &colData, true);
×
1711
        if (code) {
×
1712
          code = TSDB_CODE_INVALID_MSG;
×
1713
          TSDB_CHECK_CODE(code, lino, end);
×
1714
        }
1715
      }
1716
      if (j == nColData) {
×
1717
        colDataSetNNULL(pColData, blockStart, numOfRows);
×
1718
      }
1719
    }
1720
  } else {
1721
    uint64_t nRow = 0;
11,694,097✔
1722
    if (tDecodeU64v(pCoder, &nRow) < 0) {
11,693,296✔
1723
      code = TSDB_CODE_INVALID_MSG;
×
1724
      TSDB_CHECK_CODE(code, lino, end);
×
1725
    }
1726
    for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
25,010,926✔
1727
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
13,320,023✔
1728
      pCoder->pos += pRow->len;
13,321,146✔
1729

1730
      if (iRow == 0){
13,320,380✔
1731
#ifndef NO_UNALIGNED_ACCESS
1732
        walMeta.skey = pRow->ts;
11,693,282✔
1733
#else
1734
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1735
#endif
1736
      }
1737
      if (iRow == nRow - 1) {
13,320,106✔
1738
#ifndef NO_UNALIGNED_ACCESS
1739
        walMeta.ekey = pRow->ts;
11,692,464✔
1740
#else
1741
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
1742
#endif
1743
      }
1744

1745
      if (pRow->ts < window.skey || pRow->ts > window.ekey) {
13,321,467✔
1746
        continue;
14,928✔
1747
      }
1748
      STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
13,305,451✔
1749
      blockStart = pSlice->currentRowIdx;
13,305,209✔
1750
     
1751
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {  // reader todo test null
82,486,730✔
1752
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
69,176,052✔
1753
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
69,163,212✔
1754
        if (pColData->info.colId <= -1) {
69,163,212✔
1755
          pColData->hasNull = true;
23,559,322✔
1756
          continue;
23,559,623✔
1757
        }
1758
        int16_t colId = 0;
45,651,730✔
1759
        if (sStreamReaderInfo->isVtableStream){
45,651,458✔
1760
          STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
24,272,864✔
1761
          ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
24,277,787✔
1762
        } else {
1763
          colId = pColData->info.colId;
21,381,094✔
1764
        }
1765
        
1766
        SColVal colVal = {0};
45,658,542✔
1767
        int32_t sourceIdx = 0;
45,658,877✔
1768
        while (1) {
1769
          if (sourceIdx >= schema->numOfCols) {
124,350,138✔
1770
            break;
10,330,405✔
1771
          }
1772
          STREAM_CHECK_RET_GOTO(tRowGet(pRow, schema, sourceIdx, &colVal));
114,001,094✔
1773
          if (colVal.cid == colId) {
114,034,531✔
1774
            break;
35,343,270✔
1775
          }
1776
          sourceIdx++;
78,691,261✔
1777
        }
1778
        if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
45,673,675✔
1779
          if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL){
32,660,547✔
1780
            STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart+ numOfRows, (const char*)colVal.value.pData, colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
51,684✔
1781
            ST_TASK_TLOG("%s vtable colId:%d, i:%d, colData:%p, data:%s, len:%d, rowIndex:%d, offset:%d, uid:%" PRId64, __func__, colId, i, pColData, 
50,766✔
1782
              (const char*)colVal.value.pData, colVal.value.nData, blockStart+ numOfRows, pColData->varmeta.offset[blockStart+ numOfRows], submitTbData.uid);
1783
          } else {
1784
            STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)), !COL_VAL_IS_VALUE(&colVal)));
32,608,863✔
1785
          }
1786
        } else {
1787
          colDataSetNULL(pColData, blockStart + numOfRows);
13,013,128✔
1788
        }
1789
      }
1790
      
1791
      numOfRows++;
13,302,702✔
1792
    }
1793
  }
1794

1795
  if (numOfRows > 0) {
11,690,901✔
1796
    if (!sStreamReaderInfo->isVtableStream) {
11,690,901✔
1797
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, rsp->isCalc, submitTbData.uid, pBlock, blockStart, numOfRows, 1));
3,885,731✔
1798
    }
1799
    
1800
    SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
11,695,265✔
1801
    STREAM_CHECK_NULL_GOTO(pColData, terrno);
11,694,157✔
1802
    STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
11,694,157✔
1803

1804
    STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
11,693,586✔
1805
    ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
11,693,586✔
1806
      ", uid:%" PRId64 ", ver:%"PRId64 ", row index:%d, rows:%d", __func__, window.skey, window.ekey, 
1807
      id, submitTbData.uid, ver, pSlice->currentRowIdx, numOfRows);
1808
    pSlice->currentRowIdx += numOfRows;
11,693,859✔
1809
    pBlock->info.rows += numOfRows;
11,695,252✔
1810
  } else {
1811
    ST_TASK_DLOG("%s no valid data in time range:skey %" PRId64 ", ekey %" PRId64 ", uid:%" PRId64 ", suid:%" PRId64,
×
1812
      __func__, window.skey, window.ekey, submitTbData.uid, submitTbData.suid);
1813
  }
1814
  
1815
  if (gidHash == NULL) goto end;
11,695,249✔
1816

1817
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
2,107,204✔
1818
  if (data != NULL) {
2,105,572✔
1819
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
×
1820
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
×
1821
  } else {
1822
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
2,105,572✔
1823
  }
1824

1825
end:
12,925,205✔
1826
  if (code != 0) {                                                             \
12,926,129✔
1827
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code)); \
242✔
1828
  }
1829
  tEndDecode(pCoder);
12,926,129✔
1830
  return code;
12,924,770✔
1831
}
1832
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
12,925,582✔
1833
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
1834
  int32_t  code = 0;
12,925,582✔
1835
  int32_t  lino = 0;
12,925,582✔
1836
  SDecoder decoder = {0};
12,925,582✔
1837
  SSHashObj* gidHash = NULL;
12,927,763✔
1838
  void* pTask = sStreamReaderInfo->pTask;
12,927,763✔
1839

1840
  tDecoderInit(&decoder, data, len);
12,927,491✔
1841
  if (tStartDecode(&decoder) < 0) {
12,926,947✔
1842
    code = TSDB_CODE_INVALID_MSG;
×
1843
    TSDB_CHECK_CODE(code, lino, end);
×
1844
  }
1845

1846
  uint64_t nSubmitTbData = 0;
12,923,100✔
1847
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
12,926,690✔
1848
    code = TSDB_CODE_INVALID_MSG;
×
1849
    TSDB_CHECK_CODE(code, lino, end);
×
1850
  }
1851

1852
  if (rsp->metaBlock != NULL){
12,926,690✔
1853
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
3,336,738✔
1854
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
3,336,197✔
1855
  }
1856

1857
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
25,849,043✔
1858
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, ranges, gidHash, rsp, ver));
12,925,059✔
1859
  }
1860

1861
  tEndDecode(&decoder);
12,923,984✔
1862

1863
  if (rsp->metaBlock != NULL){
12,922,075✔
1864
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
3,335,939✔
1865
    int32_t iter = 0;
3,331,856✔
1866
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
3,332,129✔
1867
    while (px != NULL) {
5,432,257✔
1868
      WalMetaResult* pMeta = (WalMetaResult*)px;
2,103,939✔
1869
      STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
2,103,939✔
1870
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
2,106,388✔
1871
      rsp->totalRows++;
2,106,933✔
1872
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
2,106,660✔
1873
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1874
      px = tSimpleHashIterate(gidHash, px, &iter);
2,106,660✔
1875
    }
1876
  }
1877
  
1878

1879
end:
12,913,085✔
1880
  tSimpleHashCleanup(gidHash);
12,915,514✔
1881
  tDecoderClear(&decoder);
12,923,930✔
1882
  return code;
12,930,185✔
1883
}
1884

1885
static int32_t scanSubmitTbDataPre(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, 
15,314,782✔
1886
  uint64_t* gid, int64_t* uid, int32_t* numOfRows, SSTriggerWalNewRsp* rsp, int64_t ver) {
1887
  int32_t code = 0;
15,314,782✔
1888
  int32_t lino = 0;
15,314,782✔
1889
  void* pTask = sStreamReaderInfo->pTask;
15,314,782✔
1890

1891
  if (tStartDecode(pCoder) < 0) {
15,319,408✔
1892
    code = TSDB_CODE_INVALID_MSG;
×
1893
    TSDB_CHECK_CODE(code, lino, end);
×
1894
  }
1895

1896
  SSubmitTbData submitTbData = {0};
15,315,118✔
1897
  uint8_t       version = 0;
15,315,933✔
1898
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
15,315,386✔
1899
    code = TSDB_CODE_INVALID_MSG;
×
1900
    TSDB_CHECK_CODE(code, lino, end);
×
1901
  }
1902
  version = (submitTbData.flags >> 8) & 0xff;
15,315,386✔
1903
  submitTbData.flags = submitTbData.flags & 0xff;
15,315,386✔
1904

1905
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1906
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
15,315,386✔
1907
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,114,872✔
1908
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
1,114,055✔
1909
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
1,114,055✔
1910
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
1,114,710✔
1911
  }
1912

1913
  // submit data
1914
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
15,324,374✔
1915
    code = TSDB_CODE_INVALID_MSG;
×
1916
    TSDB_CHECK_CODE(code, lino, end);
×
1917
  }
1918
  if (tDecodeI64(pCoder, uid) < 0) {
15,321,939✔
1919
    code = TSDB_CODE_INVALID_MSG;
×
1920
    TSDB_CHECK_CODE(code, lino, end);
×
1921
  }
1922
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64, __func__, *uid, submitTbData.suid);
15,321,939✔
1923
  STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, *uid, gid, rsp->isCalc), TDB_CODE_SUCCESS);
15,320,916✔
1924
  if (rsp->uidHash != NULL) {
11,694,650✔
1925
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->uidHash, uid, LONG_BYTES, gid, LONG_BYTES));
9,589,683✔
1926
    ST_TASK_DLOG("%s put uid into uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%"PRIu64, __func__, *uid, submitTbData.suid, *gid);
9,590,228✔
1927
  }
1928
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
11,695,194✔
1929

1930
  if (ranges != NULL){
11,694,106✔
1931
    void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
9,590,228✔
1932
    if (timerange == NULL) goto end;;
9,587,247✔
1933
    int64_t* pRange = (int64_t*)timerange;
9,587,247✔
1934
    window.skey = pRange[0];
9,587,247✔
1935
    window.ekey = pRange[1];
9,587,247✔
1936
  }
1937
  
1938
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
11,693,031✔
1939
    code = TSDB_CODE_INVALID_MSG;
×
1940
    TSDB_CHECK_CODE(code, lino, end);
×
1941
  }
1942

1943
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,693,031✔
1944
    uint64_t nColData = 0;
×
1945
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1946
      code = TSDB_CODE_INVALID_MSG;
×
1947
      TSDB_CHECK_CODE(code, lino, end);
×
1948
    }
1949

1950
    SColData colData = {0};
×
1951
    code = tDecodeColData(version, pCoder, &colData, false);
×
1952
    if (code) {
×
1953
      code = TSDB_CODE_INVALID_MSG;
×
1954
      TSDB_CHECK_CODE(code, lino, end);
×
1955
    }
1956

1957
    if (colData.flag != HAS_VALUE) {
×
1958
      code = TSDB_CODE_INVALID_MSG;
×
1959
      TSDB_CHECK_CODE(code, lino, end);
×
1960
    }
1961
    int32_t rowStart = 0;
×
1962
    int32_t rowEnd = 0;
×
1963
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
×
1964
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, numOfRows));
×
1965
    } else {
1966
      (*numOfRows) = colData.nVal;
×
1967
    } 
1968
  } else {
1969
    uint64_t nRow = 0;
11,693,031✔
1970
    if (tDecodeU64v(pCoder, &nRow) < 0) {
11,693,816✔
1971
      code = TSDB_CODE_INVALID_MSG;
×
1972
      TSDB_CHECK_CODE(code, lino, end);
×
1973
    }
1974

1975
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) { 
11,693,816✔
1976
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
20,474,569✔
1977
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
10,882,950✔
1978
        pCoder->pos += pRow->len;
10,884,373✔
1979
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
10,885,191✔
1980
          continue;
14,928✔
1981
        }
1982
        (*numOfRows)++;
10,869,972✔
1983
      }
1984
    } else {
1985
      (*numOfRows) = nRow;
2,104,146✔
1986
    }
1987
  }
1988
  
1989
end:
15,328,205✔
1990
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
15,318,388✔
1991
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
15,315,363✔
1992
  tEndDecode(pCoder);
15,314,289✔
1993
  return code;
15,316,067✔
1994
}
1995

1996
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
15,322,517✔
1997
  int32_t  code = 0;
15,322,517✔
1998
  int32_t  lino = 0;
15,322,517✔
1999
  SDecoder decoder = {0};
15,322,517✔
2000
  void* pTask = sStreamReaderInfo->pTask;
15,324,968✔
2001

2002
  tDecoderInit(&decoder, data, len);
15,325,510✔
2003
  if (tStartDecode(&decoder) < 0) {
15,318,721✔
2004
    code = TSDB_CODE_INVALID_MSG;
×
2005
    TSDB_CHECK_CODE(code, lino, end);
×
2006
  }
2007

2008
  uint64_t nSubmitTbData = 0;
15,312,946✔
2009
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
15,317,035✔
2010
    code = TSDB_CODE_INVALID_MSG;
×
2011
    TSDB_CHECK_CODE(code, lino, end);
×
2012
  }
2013
  ST_TASK_DLOG("%s nSubmitTbData:%" PRIu64 ", ver:%"PRId64 " bodyLen:%d", __func__, nSubmitTbData, ver, len);
15,317,035✔
2014

2015
  for (int32_t i = 0; i < nSubmitTbData; i++) {
30,626,986✔
2016
    uint64_t gid = -1;
15,314,287✔
2017
    int64_t  uid = 0;
15,314,015✔
2018
    int32_t numOfRows = 0;
15,314,558✔
2019
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp, ver));
15,314,268✔
2020
    if (numOfRows <= 0) {
15,324,665✔
2021
      ST_TASK_DLOG("%s no valid data uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, ver:%"PRId64, __func__, uid, gid, numOfRows, ver);
3,634,104✔
2022
      continue;
3,634,376✔
2023
    }
2024
    rsp->totalRows += numOfRows;
11,690,561✔
2025
    rsp->totalDataRows += numOfRows;
11,690,562✔
2026

2027
    SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, &uid, LONG_BYTES);
11,689,989✔
2028
    if (pSlice != NULL) {
11,691,346✔
2029
      pSlice->numRows += numOfRows;
10,697,476✔
2030
      ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d, hash:%p %d, ver:%"PRId64, __func__, uid, gid, pSlice->numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
10,697,477✔
2031
      pSlice->gId = gid;
10,697,477✔
2032
    } else {
2033
      SStreamWalDataSlice tmp = {.gId=gid,.numRows=numOfRows,.currentRowIdx=0,.startRowIdx=0};
993,870✔
2034
      ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, hash:%p %d, ver:%"PRId64, __func__, uid, gid, tmp.numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
993,870✔
2035
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->indexHash, &uid, LONG_BYTES, &tmp, sizeof(tmp)));
993,870✔
2036
    } 
2037
  }
2038

2039
  tEndDecode(&decoder);
15,312,699✔
2040

2041
end:
15,311,866✔
2042
  tDecoderClear(&decoder);
15,312,957✔
2043
  return code;
15,327,879✔
2044
}
2045

2046
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
869,393✔
2047
  void*   pe = NULL;
869,393✔
2048
  int32_t iter = 0;
869,393✔
2049
  int32_t index = 0;
869,393✔
2050
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
1,864,110✔
2051
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
994,717✔
2052
    pInfo->startRowIdx = index;
994,717✔
2053
    pInfo->currentRowIdx = index;
994,717✔
2054
    index += pInfo->numRows;
994,717✔
2055
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
1,778,042✔
2056
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
2057
  }
2058
}
869,393✔
2059

2060
static void printIndexHash(SSHashObj* indexHash, void* pTask){
869,151✔
2061
  if (qDebugFlag & DEBUG_TRACE) {
869,151✔
2062
    void*   pe = NULL;
9,689✔
2063
    int32_t iter = 0;
9,689✔
2064
    while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
23,430✔
2065
      SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
13,741✔
2066
      ST_TASK_TLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
13,998✔
2067
      pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
2068
    }
2069
  }
2070
}
869,151✔
2071

2072
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
13,965✔
2073
  void*   pe = NULL;
13,965✔
2074
  int32_t iter = 0;
13,965✔
2075
  int32_t index = 0;
13,965✔
2076
  int32_t pIndex = 0;
13,965✔
2077
  int8_t* pIndicator = (int8_t*)pRet->pData;
13,965✔
2078
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
29,804✔
2079
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
15,839✔
2080
    pInfo->startRowIdx = index;
15,839✔
2081
    int32_t size = pInfo->numRows;
15,839✔
2082
    for (int32_t i = 0; i < pInfo->numRows; i++) {
224,752✔
2083
      if (pIndicator && !pIndicator[pIndex++]) {
208,913✔
2084
        size--;
70,626✔
2085
      }
2086
    }
2087
    pInfo->numRows = size;
15,839✔
2088
    index += pInfo->numRows;
15,839✔
2089
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
15,839✔
2090
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
2091
  }
2092
}
13,965✔
2093

2094
static int32_t prepareIndexMetaData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* resultRsp){
5,864,818✔
2095
  int32_t      code = 0;
5,864,818✔
2096
  int32_t      lino = 0;
5,864,818✔
2097
  void* pTask = sStreamReaderInfo->pTask;
5,864,818✔
2098

2099
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
5,864,786✔
2100
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
5,859,884✔
2101
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
5,157,878✔
2102
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
2103
      resultRsp->verTime = 0;
×
2104
    } else {
2105
      resultRsp->verTime = taosGetTimestampUs();
5,160,348✔
2106
    }
2107
    ST_TASK_DLOG("%s scan wal end:%s",  __func__, tstrerror(code));
5,161,695✔
2108
    code = TSDB_CODE_SUCCESS;
5,159,866✔
2109
    goto end;
5,159,866✔
2110
  }
2111
  STREAM_CHECK_RET_GOTO(code);
702,006✔
2112

2113
  while (1) {
5,961,216✔
2114
    code = walNextValidMsg(pWalReader, true);
6,663,222✔
2115
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
6,652,927✔
2116
      resultRsp->verTime = taosGetTimestampUs();
703,319✔
2117
      ST_TASK_DLOG("%s scan wal end:%s", __func__, tstrerror(code));
703,319✔
2118
      code = TSDB_CODE_SUCCESS;
703,319✔
2119
      goto end;
703,319✔
2120
    }
2121
    STREAM_CHECK_RET_GOTO(code);
5,948,760✔
2122
    resultRsp->ver = pWalReader->curVersion;
5,948,760✔
2123
    SWalCont* wCont = &pWalReader->pHead->head;
5,953,656✔
2124
    resultRsp->verTime = wCont->ingestTs;
5,951,978✔
2125
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
5,952,825✔
2126
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
5,952,657✔
2127
    int64_t ver = wCont->version;
5,953,310✔
2128
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d, msg len:%d", __func__,
5,952,800✔
2129
      ver, TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl, len);
2130
    if (wCont->msgType == TDMT_VND_SUBMIT) {
5,955,446✔
2131
      // return when getting data if there are meta data in vtable scan
2132
      if (sStreamReaderInfo->isVtableStream && resultRsp->tableBlock != NULL && ((SSDataBlock*)resultRsp->tableBlock)->info.rows > 0) {
5,728,443✔
2133
        resultRsp->ver--;
×
2134
        break;
×
2135
      }
2136
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
5,730,622✔
2137
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
5,729,231✔
2138
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp, ver));
5,730,863✔
2139
    } else {
2140
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
225,633✔
2141
    }
2142

2143
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
5,961,699✔
2144
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM || resultRsp->needReturn) {
5,961,120✔
2145
      break;
2146
    }
2147
  }
2148
  
2149
end:
1,088✔
2150
  STREAM_PRINT_LOG_END(code, lino);
5,864,122✔
2151
  return code;
5,867,269✔
2152
}
2153

2154
static int32_t prepareIndexData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, 
12,296,777✔
2155
  SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp){
2156
  int32_t      code = 0;
12,296,777✔
2157
  int32_t      lino = 0;
12,296,777✔
2158
  void* pTask = sStreamReaderInfo->pTask;
12,296,777✔
2159

2160
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
21,887,803✔
2161
    int64_t *ver = taosArrayGet(versions, i);
9,588,533✔
2162
    if (ver == NULL) continue;
9,589,681✔
2163

2164
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
9,589,681✔
2165
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
9,590,500✔
2166
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
2167
      ST_TASK_TLOG("%s not data, skip, ver:%"PRId64, __func__, *ver);
×
2168
      continue;
×
2169
    }
2170
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
9,590,500✔
2171

2172
    SWalCont* wCont = &pWalReader->pHead->head;
9,588,047✔
2173
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
9,589,166✔
2174
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
9,589,439✔
2175

2176
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp, *ver));
9,589,439✔
2177
  }
2178
  
2179
end:
12,298,139✔
2180
  return code;
12,298,139✔
2181
}
2182

2183
static int32_t filterData(SSTriggerWalNewRsp* resultRsp, SStreamTriggerReaderInfo* sStreamReaderInfo) {
869,151✔
2184
  int32_t      code = 0;
869,151✔
2185
  int32_t       lino = 0;
869,151✔
2186
  SColumnInfoData* pRet = NULL;
869,151✔
2187

2188
  int64_t totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
869,151✔
2189
  STREAM_CHECK_RET_GOTO(qStreamFilter(((SSDataBlock*)resultRsp->dataBlock), sStreamReaderInfo->pFilterInfo, &pRet));
869,151✔
2190

2191
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
869,151✔
2192
    filterIndexHash(resultRsp->indexHash, pRet);
13,965✔
2193
  }
2194

2195
end:
868,845✔
2196
  colDataDestroy(pRet);
868,845✔
2197
  taosMemoryFree(pRet);
869,151✔
2198
  return code;
869,151✔
2199
}
2200

2201
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
5,863,048✔
2202
                                    SSTriggerWalNewRsp* resultRsp) {
2203
  int32_t      code = 0;
5,863,048✔
2204
  int32_t      lino = 0;
5,863,048✔
2205
  void* pTask = sStreamReaderInfo->pTask;
5,863,048✔
2206
                                        
2207
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
5,864,458✔
2208
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
5,862,991✔
2209
  blockDataEmpty(resultRsp->dataBlock);
5,862,991✔
2210
  blockDataEmpty(resultRsp->metaBlock);
5,860,819✔
2211
  int64_t lastVer = resultRsp->ver;                                      
5,859,365✔
2212
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp));
5,859,141✔
2213
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
5,863,730✔
2214

2215
  buildIndexHash(resultRsp->indexHash, pTask);
179,757✔
2216
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
179,757✔
2217
  while(lastVer < resultRsp->ver) {
3,568,727✔
2218
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
3,390,064✔
2219
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
3,386,794✔
2220
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
49,528✔
2221
      continue;
49,528✔
2222
    }
2223
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
3,338,626✔
2224
    SWalCont* wCont = &pWalReader->pHead->head;
3,335,580✔
2225
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
3,335,307✔
2226
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
3,334,762✔
2227
    ST_TASK_DLOG("process wal ver:%" PRId64 ", type:%d, bodyLen:%d", wCont->version, wCont->msgType, bodyLen);
3,334,762✔
2228
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
3,336,143✔
2229
  }
2230

2231
  int32_t metaRows = resultRsp->totalRows - ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
179,515✔
2232
  STREAM_CHECK_RET_GOTO(filterData(resultRsp, sStreamReaderInfo));
179,515✔
2233
  resultRsp->totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows + metaRows;
179,515✔
2234

2235
end:
5,862,304✔
2236
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
5,862,304✔
2237
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
2238
  walCloseReader(pWalReader);
5,862,368✔
2239
  return code;
5,863,728✔
2240
}
2241

2242
static int32_t processWalVerDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
12,297,354✔
2243
                                    SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
2244
  int32_t      code = 0;
12,297,354✔
2245
  int32_t      lino = 0;
12,297,354✔
2246

2247
  void* pTask = sStreamReaderInfo->pTask;
12,297,354✔
2248
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
12,297,834✔
2249
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
12,296,506✔
2250
  
2251
  if (taosArrayGetSize(versions) > 0) {
12,296,506✔
2252
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
689,363✔
2253
  }
2254
  
2255
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
12,298,455✔
2256
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
12,297,292✔
2257

2258
  ST_TASK_TLOG("%s index hash:%p %d", __func__, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash));
689,636✔
2259
  buildIndexHash(rsp->indexHash, pTask);
689,636✔
2260

2261
  blockDataEmpty(rsp->dataBlock);
689,636✔
2262
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dataBlock, rsp->totalRows));
689,636✔
2263

2264
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
10,277,937✔
2265
    int64_t *ver = taosArrayGet(versions, i);
9,589,136✔
2266
    if (ver == NULL) continue;
9,588,590✔
2267
    ST_TASK_TLOG("vgId:%d %s scan wal process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, *ver, walGetAppliedVer(pWalReader->pWal));
9,588,590✔
2268

2269
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
9,588,590✔
2270
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
9,588,881✔
2271
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
2272
      continue;
×
2273
    }
2274
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
9,589,427✔
2275
    SWalCont* wCont = &pWalReader->pHead->head;
9,589,954✔
2276
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
9,590,772✔
2277
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
9,590,772✔
2278

2279
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
9,590,772✔
2280
  }
2281
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
2282
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
689,091✔
2283
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
689,636✔
2284

2285
end:
12,297,274✔
2286
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
12,297,274✔
2287
            rsp->totalRows, rsp->ver, walGetAppliedVer(pWalReader->pWal));
2288
  walCloseReader(pWalReader);
12,300,309✔
2289
  return code;
12,299,207✔
2290
}
2291

2292
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas, SStorageAPI* api) {
676,238✔
2293
  int32_t code = 0;
676,238✔
2294
  int32_t lino = 0;
676,238✔
2295
  SMetaReader metaReader = {0};
676,238✔
2296
  *schemas = taosArrayInit(8, sizeof(SSchema));
676,238✔
2297
  STREAM_CHECK_NULL_GOTO(*schemas, terrno);
675,391✔
2298
  
2299
  api->metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api->metaFn);
675,391✔
2300
  STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, uid));
676,238✔
2301

2302
  SSchemaWrapper* sSchemaWrapper = NULL;
672,850✔
2303
  if (metaReader.me.type == TD_CHILD_TABLE) {
672,850✔
2304
    int64_t suid = metaReader.me.ctbEntry.suid;
672,850✔
2305
    tDecoderClear(&metaReader.coder);
672,850✔
2306
    STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, suid));
672,850✔
2307
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
672,850✔
2308
  } else if (metaReader.me.type == TD_NORMAL_TABLE) {
×
2309
    sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
×
2310
  } else {
2311
    qError("invalid table type:%d", metaReader.me.type);
×
2312
  }
2313

2314
  for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
3,990,210✔
2315
    SSchema* s = sSchemaWrapper->pSchema + j;
3,317,360✔
2316
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
6,634,720✔
2317
  }
2318

2319
end:
676,238✔
2320
  api->metaReaderFn.clearReader(&metaReader);
676,238✔
2321
  STREAM_PRINT_LOG_END(code, lino);
676,238✔
2322
  if (code != 0)  {
676,238✔
2323
    taosArrayDestroy(*schemas);
3,388✔
2324
    *schemas = NULL;
3,388✔
2325
  }
2326
  return code;
676,238✔
2327
}
2328

2329
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
672,850✔
2330
  int32_t code = 0;
672,850✔
2331
  int32_t lino = 0;
672,850✔
2332
  size_t  schemaLen = taosArrayGetSize(schemas);
672,850✔
2333
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
672,850✔
2334
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
2,641,980✔
2335
    col_id_t* id = taosArrayGet(cols, i);
1,969,130✔
2336
    STREAM_CHECK_NULL_GOTO(id, terrno);
1,969,130✔
2337
    for (size_t i = 0; i < schemaLen; i++) {
5,176,253✔
2338
      SSchema* s = taosArrayGet(schemas, i);
5,176,253✔
2339
      STREAM_CHECK_NULL_GOTO(s, terrno);
5,176,253✔
2340
      if (*id == s->colId) {
5,176,253✔
2341
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
1,969,130✔
2342
        break;
1,969,130✔
2343
      }
2344
    }
2345
  }
2346
  taosArrayPopFrontBatch(schemas, schemaLen);
672,850✔
2347

2348
end:
672,850✔
2349
  return code;
672,850✔
2350
}
2351

2352
static int32_t createTSAndCondition(int64_t start, int64_t end, SLogicConditionNode** pCond,
×
2353
                                    STargetNode* pTargetNodeTs) {
2354
  int32_t code = 0;
×
2355
  int32_t lino = 0;
×
2356

2357
  SColumnNode*         pCol = NULL;
×
2358
  SColumnNode*         pCol1 = NULL;
×
2359
  SValueNode*          pVal = NULL;
×
2360
  SValueNode*          pVal1 = NULL;
×
2361
  SOperatorNode*       op = NULL;
×
2362
  SOperatorNode*       op1 = NULL;
×
2363
  SLogicConditionNode* cond = NULL;
×
2364

2365
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol));
×
2366
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
×
2367
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
×
2368
  pCol->node.resType.bytes = LONG_BYTES;
×
2369
  pCol->slotId = pTargetNodeTs->slotId;
×
2370
  pCol->dataBlockId = pTargetNodeTs->dataBlockId;
×
2371

2372
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pCol, (SNode**)&pCol1));
×
2373

2374
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal));
×
2375
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
2376
  pVal->node.resType.bytes = LONG_BYTES;
×
2377
  pVal->datum.i = start;
×
2378
  pVal->typeData = start;
×
2379

2380
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pVal, (SNode**)&pVal1));
×
2381
  pVal1->datum.i = end;
×
2382
  pVal1->typeData = end;
×
2383

2384
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op));
×
2385
  op->opType = OP_TYPE_GREATER_EQUAL;
×
2386
  op->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
2387
  op->node.resType.bytes = CHAR_BYTES;
×
2388
  op->pLeft = (SNode*)pCol;
×
2389
  op->pRight = (SNode*)pVal;
×
2390
  pCol = NULL;
×
2391
  pVal = NULL;
×
2392

2393
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op1));
×
2394
  op1->opType = OP_TYPE_LOWER_EQUAL;
×
2395
  op1->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
2396
  op1->node.resType.bytes = CHAR_BYTES;
×
2397
  op1->pLeft = (SNode*)pCol1;
×
2398
  op1->pRight = (SNode*)pVal1;
×
2399
  pCol1 = NULL;
×
2400
  pVal1 = NULL;
×
2401

2402
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
×
2403
  cond->condType = LOGIC_COND_TYPE_AND;
×
2404
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
2405
  cond->node.resType.bytes = CHAR_BYTES;
×
2406
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
×
2407
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op));
×
2408
  op = NULL;
×
2409
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op1));
×
2410
  op1 = NULL;
×
2411

2412
  *pCond = cond;
×
2413

2414
end:
×
2415
  if (code != 0) {
×
2416
    nodesDestroyNode((SNode*)pCol);
×
2417
    nodesDestroyNode((SNode*)pCol1);
×
2418
    nodesDestroyNode((SNode*)pVal);
×
2419
    nodesDestroyNode((SNode*)pVal1);
×
2420
    nodesDestroyNode((SNode*)op);
×
2421
    nodesDestroyNode((SNode*)op1);
×
2422
    nodesDestroyNode((SNode*)cond);
×
2423
  }
2424
  STREAM_PRINT_LOG_END(code, lino);
×
2425

2426
  return code;
×
2427
}
2428

2429
/*
2430
static int32_t createExternalConditions(SStreamRuntimeFuncInfo* data, SLogicConditionNode** pCond, STargetNode* pTargetNodeTs, STimeRangeNode* node) {
2431
  int32_t              code = 0;
2432
  int32_t              lino = 0;
2433
  SLogicConditionNode* pAndCondition = NULL;
2434
  SLogicConditionNode* cond = NULL;
2435

2436
  if (pTargetNodeTs == NULL) {
2437
    vError("stream reader %s no ts column", __func__);
2438
    return TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN;
2439
  }
2440
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
2441
  cond->condType = LOGIC_COND_TYPE_OR;
2442
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
2443
  cond->node.resType.bytes = CHAR_BYTES;
2444
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
2445

2446
  for (int i = 0; i < taosArrayGetSize(data->pStreamPesudoFuncVals); ++i) {
2447
    data->curIdx = i;
2448

2449
    SReadHandle handle = {0};
2450
    calcTimeRange(node, data, &handle.winRange, &handle.winRangeValid);
2451
    if (!handle.winRangeValid) {
2452
      stError("stream reader %s invalid time range, skey:%" PRId64 ", ekey:%" PRId64, __func__, handle.winRange.skey,
2453
              handle.winRange.ekey);
2454
      continue;
2455
    }
2456
    STREAM_CHECK_RET_GOTO(createTSAndCondition(handle.winRange.skey, handle.winRange.ekey, &pAndCondition, pTargetNodeTs));
2457
    stDebug("%s create condition skey:%" PRId64 ", eksy:%" PRId64, __func__, handle.winRange.skey, handle.winRange.ekey);
2458
    STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)pAndCondition));
2459
    pAndCondition = NULL;
2460
  }
2461

2462
  *pCond = cond;
2463

2464
end:
2465
  if (code != 0) {
2466
    nodesDestroyNode((SNode*)pAndCondition);
2467
    nodesDestroyNode((SNode*)cond);
2468
  }
2469
  STREAM_PRINT_LOG_END(code, lino);
2470

2471
  return code;
2472
}
2473
*/
2474

2475
static int32_t processCalaTimeRange(SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo, SResFetchReq* req,
688,635✔
2476
                                    STimeRangeNode* node, SReadHandle* handle, bool isExtWin) {
2477
  int32_t code = 0;
688,635✔
2478
  int32_t lino = 0;
688,635✔
2479
  void* pTask = sStreamReaderCalcInfo->pTask;
688,635✔
2480
  STimeWindow* pWin = isExtWin ? &handle->extWinRange : &handle->winRange;
688,635✔
2481
  bool* pValid = isExtWin ? &handle->extWinRangeValid : &handle->winRangeValid;
688,635✔
2482
  
2483
  if (req->pStRtFuncInfo->withExternalWindow) {
688,635✔
2484
    sStreamReaderCalcInfo->tmpRtFuncInfo.curIdx = 0;
518,105✔
2485
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
518,105✔
2486
    sStreamReaderCalcInfo->tmpRtFuncInfo.isWindowTrigger = req->pStRtFuncInfo->isWindowTrigger;
518,105✔
2487
    sStreamReaderCalcInfo->tmpRtFuncInfo.precision = req->pStRtFuncInfo->precision;
518,105✔
2488

2489
    SSTriggerCalcParam* pFirst = NULL;
518,105✔
2490
    SSTriggerCalcParam* pLast = NULL;
518,105✔
2491
    if (req->pStRtFuncInfo->isMultiGroupCalc) {
518,105✔
2492
      SSTriggerGroupReadInfo* pGrp = taosArrayGet(req->pStRtFuncInfo->curGrpRead, 0);
×
2493
      pFirst = &pGrp->firstParam;
×
2494
      pLast = &pGrp->lastParam;
×
2495
    } else {
2496
      pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
518,105✔
2497
      pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
518,105✔
2498
      STREAM_CHECK_NULL_GOTO(pFirst, terrno);
518,105✔
2499
      STREAM_CHECK_NULL_GOTO(pLast, terrno);
518,105✔
2500
    }
2501

2502
    if (!node->needCalc) {
518,105✔
2503
      pWin->skey = pFirst->wstart;
371,950✔
2504
      pWin->ekey = pLast->wend;
371,950✔
2505
      *pValid = true;
371,950✔
2506
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
371,950✔
2507
        pWin->ekey--;
224,818✔
2508
      }
2509
    } else {
2510
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
146,155✔
2511
      memcpy(pTmp, pFirst, sizeof(*pTmp));
146,155✔
2512

2513
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 1));
146,155✔
2514
      if (*pValid) {
146,155✔
2515
        int64_t skey = pWin->skey;
146,155✔
2516

2517
        memcpy(pTmp, pLast, sizeof(*pTmp));
146,155✔
2518
        STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 2));
146,155✔
2519

2520
        if (*pValid) {
146,155✔
2521
          pWin->skey = skey;
146,155✔
2522
        }
2523
      }
2524
      pWin->ekey--;
146,155✔
2525
    }
2526
  } else {
2527
    if (!node->needCalc) {
170,530✔
2528
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
120,770✔
2529
      pWin->skey = pCurr->wstart;
120,770✔
2530
      pWin->ekey = pCurr->wend;
120,770✔
2531
      *pValid = true;
120,770✔
2532
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
120,770✔
2533
        pWin->ekey--;
64,582✔
2534
      }
2535
    } else {
2536
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, pWin, pValid, 3));
49,760✔
2537
      pWin->ekey--;
49,760✔
2538
    }
2539
  }
2540

2541
  ST_TASK_DLOG("%s type:%s, withExternalWindow:%d, skey:%" PRId64 ", ekey:%" PRId64 ", validRange:%d", 
688,635✔
2542
      __func__, isExtWin ? "interp range" : "scan time range", req->pStRtFuncInfo->withExternalWindow, pWin->skey, pWin->ekey, *pValid);
2543

2544
end:
5,452✔
2545

2546
  if (code) {
688,635✔
2547
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2548
  }
2549
  
2550
  return code;
688,635✔
2551
}
2552

2553
static int32_t createDataBlockTsUid(SSDataBlock** pBlockRet, uint32_t numOfRows) {
669,444✔
2554
  int32_t      code = 0;
669,444✔
2555
  int32_t      lino = 0;
669,444✔
2556
  SSDataBlock* pBlock = NULL;
669,444✔
2557
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
669,444✔
2558
  SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID);
669,444✔
2559
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
669,444✔
2560
  idata = createColumnInfoData(TSDB_DATA_TYPE_BIGINT, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID + 1);
669,444✔
2561
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
669,444✔
2562
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, numOfRows));
669,444✔
2563

2564
end:
669,444✔
2565
  STREAM_PRINT_LOG_END(code, lino)
669,444✔
2566
  if (code != TSDB_CODE_SUCCESS) {
669,444✔
2567
    blockDataDestroy(pBlock);
×
2568
    pBlock = NULL;
×
2569
  }
2570
  *pBlockRet = pBlock;
669,444✔
2571
  return code;
669,444✔
2572
}
2573

2574
static int32_t processTsOutPutAllTables(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
402,852✔
2575
  int32_t code = 0;
402,852✔
2576
  int32_t lino = 0;
402,852✔
2577
  void* pTask = sStreamReaderInfo->pTask;
402,852✔
2578

2579
  tsRsp->tsInfo = taosArrayInit(pResBlock->info.rows, sizeof(STsInfo));
402,852✔
2580
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
402,852✔
2581
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
402,852✔
2582
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
402,852✔
2583
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
1,122,913✔
2584
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
1,440,122✔
2585
      continue;
×
2586
    }
2587
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
720,061✔
2588
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
720,061✔
2589
    if (order == TSDB_ORDER_ASC) {
720,061✔
2590
      tsInfo->ts = INT64_MAX;
394,825✔
2591
    } else {
2592
      tsInfo->ts = INT64_MIN;
325,236✔
2593
    }
2594
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
720,061✔
2595
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
720,061✔
2596
      tsInfo->ts = ts;
394,825✔
2597
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
325,236✔
2598
      tsInfo->ts = ts;
325,236✔
2599
    }
2600
    tsInfo->gId = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
720,061✔
2601
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
720,061✔
2602
  }
2603

2604
end:
402,852✔
2605
  return code;
402,852✔
2606
}
2607

2608
static int32_t processTsOutPutOneGroup(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
127,548✔
2609
  int32_t code = 0;
127,548✔
2610
  int32_t lino = 0;
127,548✔
2611
  void* pTask = sStreamReaderInfo->pTask;
127,548✔
2612

2613
  tsRsp->tsInfo = taosArrayInit(1, sizeof(STsInfo));
127,548✔
2614
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
127,548✔
2615
  STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
127,548✔
2616
  STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
127,548✔
2617
  if (order == TSDB_ORDER_ASC) {
127,548✔
2618
    tsInfo->ts = INT64_MAX;
97,651✔
2619
  } else {
2620
    tsInfo->ts = INT64_MIN;
29,897✔
2621
  }
2622

2623
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
127,548✔
2624
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
127,276✔
2625
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
274,942✔
2626
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
295,060✔
2627
      continue;
×
2628
    }
2629
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
147,666✔
2630
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
147,122✔
2631
      tsInfo->ts = ts;
97,379✔
2632
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
49,743✔
2633
      tsInfo->ts = ts;
29,897✔
2634
    }
2635
  }
2636
  int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, 0);
127,548✔
2637
  tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
127,548✔
2638
  ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
127,548✔
2639

2640
end:
35,669✔
2641
  return code;
127,548✔
2642
}
2643

2644
static int32_t processTsOutPutAllGroups(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
10,333✔
2645
  int32_t code = 0;
10,333✔
2646
  int32_t lino = 0;
10,333✔
2647
  STableKeyInfo* pList = NULL;
10,333✔
2648
  StreamTableListInfo     tableInfo = {0};
10,333✔
2649

2650
  void* pTask = sStreamReaderInfo->pTask;
10,333✔
2651
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
10,333✔
2652

2653
  SSHashObj*   uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
10,333✔
2654
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
10,333✔
2655
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
10,333✔
2656
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
10,333✔
2657
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
32,582✔
2658
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
44,498✔
2659
      continue;
×
2660
    }
2661
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
22,249✔
2662
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
22,249✔
2663
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &uid, LONG_BYTES, &ts, LONG_BYTES));
22,249✔
2664
  }
2665
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
10,333✔
2666
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
10,333✔
2667
  while (true) {
21,315✔
2668
    int32_t        pNum = 0;
31,648✔
2669
    int64_t        suid = 0;
31,648✔
2670
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
31,648✔
2671
    if(pNum == 0) break;
31,648✔
2672
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
22,018✔
2673
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
22,018✔
2674
    if (order == TSDB_ORDER_ASC) {
22,018✔
2675
      tsInfo->ts = INT64_MAX;
10,428✔
2676
    } else {
2677
      tsInfo->ts = INT64_MIN;
11,590✔
2678
    }
2679
    for (int32_t i = 0; i < pNum; i++) {
43,783✔
2680
      int64_t uid = pList[i].uid;
22,468✔
2681
      int64_t *ts = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
22,468✔
2682
      STREAM_CHECK_NULL_GOTO(ts, terrno);
22,468✔
2683
      if (order == TSDB_ORDER_ASC && *ts < tsInfo->ts) {
21,765✔
2684
        tsInfo->ts = *ts;
10,428✔
2685
      } else if (order == TSDB_ORDER_DESC && *ts > tsInfo->ts) {
11,337✔
2686
        tsInfo->ts = *ts;
10,887✔
2687
      }
2688
    }
2689
    int64_t uid = pList[0].uid;
21,315✔
2690
    tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
21,315✔
2691
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
21,315✔
2692
    taosMemoryFreeClear(pList);
21,315✔
2693
  }
2694

2695
end:
10,333✔
2696
  qStreamDestroyTableInfo(&tableInfo);
10,333✔
2697
  taosMemoryFreeClear(pList);
10,333✔
2698
  tSimpleHashCleanup(uidTsHash);
10,333✔
2699
  return code;
10,333✔
2700
}
2701

2702
// static bool stReaderTaskWaitQuit(SStreamTask* pTask) { return taosHasRWWFlag(&pTask->entryLock); }
2703

2704
static int32_t getAllTs(SVnode* pVnode, SSDataBlock*  pResBlock, SStreamReaderTaskInner* pTaskInner, STableKeyInfo* pList, int32_t pNum) {
515,000✔
2705
  int32_t code = 0;
515,000✔
2706
  int32_t lino = 0;
515,000✔
2707

2708
  stDebug("%s getAllTs enter: pNum:%d suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" verRange:[%"PRId64",%"PRId64"]",
515,000✔
2709
          pTaskInner->idStr, pNum, pTaskInner->options->suid, pTaskInner->options->order,
2710
          pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey,
2711
          (int64_t)-1, pTaskInner->options->ver);
2712
  for (int32_t i = 0; i < pNum; i++) {
1,966,043✔
2713
    stDebug("%s getAllTs table[%d]: uid:%"PRId64, pTaskInner->idStr, i, pList[i].uid);
1,451,043✔
2714
  }
2715

2716
  STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdCreateFirstLastTsIter(pVnode, &pTaskInner->options->twindows, &(SVersionRange){.minVer = -1, .maxVer = pTaskInner->options->ver},
515,000✔
2717
                                                pTaskInner->options->suid, pList, pNum, pTaskInner->options->order, &pTaskInner->pReader, pTaskInner->idStr));
2718
  bool hasNext = true;
515,000✔
2719
  int32_t iterCount = 0;
515,000✔
2720
  while(1){
2721
    STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdNextFirstLastTsBlock(pTaskInner->pReader, pResBlock, &hasNext));
515,000✔
2722
    stDebug("%s getAllTs iter[%d]: hasNext:%d pResBlock->info.rows:%"PRId64, pTaskInner->idStr, iterCount++, hasNext, pResBlock->info.rows);
514,711✔
2723
    STREAM_CHECK_CONDITION_GOTO(!hasNext, TDB_CODE_SUCCESS);
515,000✔
2724
  }
2725

2726
end:
515,000✔
2727
  stDebug("%s getAllTs done: code:%d pResBlock->info.rows:%"PRId64, pTaskInner->idStr, code, pResBlock ? pResBlock->info.rows : -1);
515,000✔
2728
  pTaskInner->storageApi->tsdReader.tsdDestroyFirstLastTsIter(pTaskInner->pReader);
515,000✔
2729
  pTaskInner->pReader = NULL;
514,279✔
2730
  return code;
514,551✔
2731
}
2732

2733
static int32_t processTsVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
328,744✔
2734
                                  SStreamReaderTaskInner* pTaskInner) {
2735
  int32_t code = 0;
328,744✔
2736
  int32_t lino = 0;
328,744✔
2737
  STableKeyInfo* pList = NULL;
328,744✔
2738
  StreamTableListInfo     tableInfo = {0};
328,744✔
2739

2740
  void* pTask = sStreamReaderInfo->pTask;
328,744✔
2741
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
328,744✔
2742

2743
  SSDataBlock*  pResBlock = NULL;
328,744✔
2744
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, qStreamGetTableListNum(sStreamReaderInfo)));
328,744✔
2745

2746
  while (true) {
174,300✔
2747
    int32_t        pNum = 0;
503,044✔
2748
    int64_t        suid = 0;
503,044✔
2749
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
503,044✔
2750
    if(pNum == 0) break;
503,044✔
2751
    pTaskInner->options->suid = suid;
174,300✔
2752
    STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
174,300✔
2753
    taosMemoryFreeClear(pList);
174,300✔
2754
  }
2755

2756
  STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, pTaskInner->options->order));
328,744✔
2757

2758
end:
328,744✔
2759
  qStreamDestroyTableInfo(&tableInfo);
328,744✔
2760
  taosMemoryFreeClear(pList);
328,744✔
2761
  blockDataDestroy(pResBlock);
328,744✔
2762
  STREAM_PRINT_LOG_END_WITHID(code, lino);
328,744✔
2763
  return code;
328,744✔
2764
}
2765

2766
static int32_t processTsNonVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
327,039✔
2767
                                  SStreamReaderTaskInner* pTaskInner) {
2768
  int32_t code = 0;
327,039✔
2769
  int32_t lino = 0;
327,039✔
2770
  STableKeyInfo* pList = NULL;
327,039✔
2771
  void* pTask = sStreamReaderInfo->pTask;
327,039✔
2772

2773
  SSDataBlock*  pResBlock = NULL;
327,039✔
2774

2775
  int32_t        pNum = 0;
327,039✔
2776
  int64_t        suid = 0;
327,039✔
2777
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, 0, &pList, &pNum));
327,039✔
2778
  ST_TASK_DLOG("vgId:%d %s qStreamGetTableList returned pNum:%d", TD_VID(pVnode), __func__, pNum);
327,039✔
2779
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
327,039✔
2780
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
288,852✔
2781

2782
  pTaskInner->options->suid = sStreamReaderInfo->suid;
288,852✔
2783
  ST_TASK_DLOG("vgId:%d %s calling getAllTs: suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" ver:%"PRId64,
288,852✔
2784
               TD_VID(pVnode), __func__, pTaskInner->options->suid, pTaskInner->options->order,
2785
               pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey, pTaskInner->options->ver);
2786
  STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
288,852✔
2787
  ST_TASK_DLOG("vgId:%d %s getAllTs done: pResBlock rows:%"PRId64, TD_VID(pVnode), __func__, pResBlock->info.rows);
288,403✔
2788
  int32_t order = pTaskInner->options->order;
288,403✔
2789
  if (pResBlock->info.rows == 0 && sStreamReaderInfo->groupByTbname) {
288,852✔
2790
    tsRsp->tsInfo = taosArrayInit(pNum, sizeof(STsInfo));
43,613✔
2791
    STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
43,613✔
2792
    for (int32_t i = 0; i < pNum; i++) {
160,522✔
2793
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
116,909✔
2794
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
116,909✔
2795
      tsInfo->gId = pList[i].uid;
116,909✔
2796
      tsInfo->ts = 0;
116,909✔
2797
      ST_TASK_DLOG("%s no data but return gId (uid):%" PRIu64 " for tbname partition", __func__, tsInfo->gId);
116,909✔
2798
    }
2799
    goto end;
43,613✔
2800
  }
2801

2802
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
244,695✔
2803

2804
  if (sStreamReaderInfo->groupByTbname) {
160,141✔
2805
    STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, order));
74,108✔
2806
  } else if (sStreamReaderInfo->partitionCols == NULL) {
86,033✔
2807
    STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order));
75,700✔
2808
  } else {
2809
    STREAM_CHECK_RET_GOTO(processTsOutPutAllGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
10,333✔
2810
  }                             
2811
end:
326,777✔
2812
  blockDataDestroy(pResBlock);
327,039✔
2813
  taosMemoryFreeClear(pList);
327,039✔
2814
  STREAM_PRINT_LOG_END_WITHID(code, lino);
327,039✔
2815
  return code;
327,039✔
2816
}
2817

2818
static int32_t processTsOnce(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
71,554✔
2819
                                  SStreamReaderTaskInner* pTaskInner, uint64_t gid) {
2820
  int32_t code = 0;
71,554✔
2821
  int32_t lino = 0;
71,554✔
2822
  STableKeyInfo* pList = NULL;
71,554✔
2823
  void* pTask = sStreamReaderInfo->pTask;
71,554✔
2824
  
2825
  SSDataBlock*  pResBlock = NULL;
71,554✔
2826

2827
  int32_t        pNum = 0;
71,554✔
2828
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, gid, &pList, &pNum));
71,554✔
2829
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
71,554✔
2830
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
51,848✔
2831

2832
  pTaskInner->options->suid = sStreamReaderInfo->suid;
51,848✔
2833
  STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
51,848✔
2834
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
51,848✔
2835
  int32_t order = pTaskInner->options->order;
51,848✔
2836

2837
  STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order));
51,848✔
2838
end:
71,554✔
2839
  blockDataDestroy(pResBlock);
71,554✔
2840
  taosMemoryFreeClear(pList);
71,554✔
2841
  STREAM_PRINT_LOG_END_WITHID(code, lino);
71,554✔
2842
  return code;
71,554✔
2843
}
2844

2845
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
655,783✔
2846
                                  SStreamReaderTaskInner* pTaskInner) {
2847
  if (sStreamReaderInfo->isVtableStream) {
655,783✔
2848
    return processTsVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
328,744✔
2849
  }
2850

2851
  return processTsNonVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
327,039✔
2852
}
2853

2854
static int32_t vnodeProcessStreamSetTableReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
149,650✔
2855
  int32_t code = 0;
149,650✔
2856
  int32_t lino = 0;
149,650✔
2857
  void*   buf = NULL;
149,650✔
2858
  size_t  size = 0;
149,650✔
2859
  void* pTask = sStreamReaderInfo->pTask;
149,650✔
2860

2861
  ST_TASK_DLOG("vgId:%d %s start, trigger hash size:%d, calc hash size:%d, appver:%"PRId64, TD_VID(pVnode), __func__,
149,650✔
2862
                tSimpleHashGetSize(req->setTableReq.uidInfoTrigger), tSimpleHashGetSize(req->setTableReq.uidInfoCalc), pVnode->state.applied);
2863

2864
  taosWLockLatch(&sStreamReaderInfo->lock);
149,650✔
2865
  TSWAP(sStreamReaderInfo->uidHashTrigger, req->setTableReq.uidInfoTrigger);
149,650✔
2866
  TSWAP(sStreamReaderInfo->uidHashCalc, req->setTableReq.uidInfoCalc);
149,650✔
2867
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashTrigger, TSDB_CODE_INVALID_PARA);
149,650✔
2868
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashCalc, TSDB_CODE_INVALID_PARA);
149,650✔
2869

2870
  qStreamClearTableInfo(&sStreamReaderInfo->vSetTableList);
149,650✔
2871
  STREAM_CHECK_RET_GOTO(initStreamTableListInfo(&sStreamReaderInfo->vSetTableList));
149,650✔
2872
  STREAM_CHECK_RET_GOTO(qBuildVTableList(sStreamReaderInfo));
149,650✔
2873
end:
149,650✔
2874
  taosWUnLockLatch(&sStreamReaderInfo->lock);
149,650✔
2875
  STREAM_PRINT_LOG_END_WITHID(code, lino);
149,650✔
2876
  SRpcMsg rsp = {
149,650✔
2877
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2878
  tmsgSendRsp(&rsp);
149,650✔
2879
  return code;
149,650✔
2880
}
2881

2882
static int32_t vnodeProcessStreamLastTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
361,648✔
2883
  int32_t                 code = 0;
361,648✔
2884
  int32_t                 lino = 0;
361,648✔
2885
  SStreamReaderTaskInner* pTaskInner = NULL;
361,648✔
2886
  SStreamTsResponse       tsRsp = {0};
361,889✔
2887
  void*                   buf = NULL;
361,889✔
2888
  size_t                  size = 0;
361,889✔
2889

2890
  void* pTask = sStreamReaderInfo->pTask;
361,889✔
2891

2892
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
361,889✔
2893

2894
  BUILD_OPTION(options, 0, sStreamReaderInfo->tableList.version, TSDB_ORDER_DESC, INT64_MIN, INT64_MAX, NULL, false, NULL);
361,889✔
2895
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
361,889✔
2896

2897
  tsRsp.ver = sStreamReaderInfo->tableList.version + 1;
361,889✔
2898

2899
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
361,889✔
2900
  
2901
end:
361,889✔
2902
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
361,889✔
2903
  code = buildTsRsp(&tsRsp, &buf, &size);
361,889✔
2904
  STREAM_PRINT_LOG_END_WITHID(code, lino);
361,889✔
2905
  SRpcMsg rsp = {
362,143✔
2906
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2907
  tmsgSendRsp(&rsp);
361,889✔
2908
  taosArrayDestroy(tsRsp.tsInfo);
361,889✔
2909
  taosMemoryFree(pTaskInner);
361,889✔
2910
  return code;
361,616✔
2911
}
2912

2913
static int32_t vnodeProcessStreamFirstTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
365,448✔
2914
  int32_t                 code = 0;
365,448✔
2915
  int32_t                 lino = 0;
365,448✔
2916
  SStreamReaderTaskInner* pTaskInner = NULL;
365,448✔
2917
  SStreamTsResponse       tsRsp = {0};
365,448✔
2918
  void*                   buf = NULL;
365,448✔
2919
  size_t                  size = 0;
365,448✔
2920

2921
  void* pTask = sStreamReaderInfo->pTask;
365,448✔
2922
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64
365,175✔
2923
               " applied:%"PRId64" tableListNum:%d isVtable:%d groupByTbname:%d partitionCols:%p",
2924
               TD_VID(pVnode), __func__, req->firstTsReq.startTime, req->firstTsReq.ver, req->firstTsReq.gid,
2925
               pVnode->state.applied, qStreamGetTableListNum(sStreamReaderInfo),
2926
               sStreamReaderInfo->isVtableStream, sStreamReaderInfo->groupByTbname, sStreamReaderInfo->partitionCols);
2927
  int32_t        pNum = 0;
365,175✔
2928

2929
  tsRsp.ver = pVnode->state.applied;
365,175✔
2930

2931
  BUILD_OPTION(options, 0, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, NULL, false, NULL);
365,448✔
2932
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
365,448✔
2933

2934
  if (req->firstTsReq.gid != 0) {
365,448✔
2935
    STREAM_CHECK_RET_GOTO(processTsOnce(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner, req->firstTsReq.gid));
71,554✔
2936
  } else {
2937
    STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
293,894✔
2938
  }
2939

2940
end:
365,448✔
2941
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
365,448✔
2942
  code = buildTsRsp(&tsRsp, &buf, &size);
365,448✔
2943
  STREAM_PRINT_LOG_END_WITHID(code, lino);
365,448✔
2944
  SRpcMsg rsp = {
365,448✔
2945
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2946
  tmsgSendRsp(&rsp);
365,176✔
2947
  taosArrayDestroy(tsRsp.tsInfo);
365,448✔
2948
  taosMemoryFree(pTaskInner);
365,448✔
2949
  return code;
365,448✔
2950
}
2951

2952
static int32_t vnodeProcessStreamTsdbMetaReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
531,671✔
2953
  int32_t code = 0;
531,671✔
2954
  int32_t lino = 0;
531,671✔
2955
  void*   buf = NULL;
531,671✔
2956
  size_t  size = 0;
531,671✔
2957
  STableKeyInfo* pList = NULL;
531,671✔
2958

2959
  void* pTask = sStreamReaderInfo->pTask;
531,671✔
2960
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",gid:%" PRId64, TD_VID(pVnode),
531,671✔
2961
               __func__, req->tsdbMetaReq.ver, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime,
2962
               req->tsdbMetaReq.gid);
2963

2964
  SStreamReaderTaskInner* pTaskInner = NULL;
531,671✔
2965
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_META);
531,671✔
2966

2967
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
531,671✔
2968
    int32_t        pNum = 0;
531,671✔
2969
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbMetaReq.gid, &pList, &pNum));
531,671✔
2970
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, 
531,671✔
2971
                          sStreamReaderInfo->tsSchemas, true, NULL);
2972
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, pList, pNum, &sStreamReaderInfo->storageApi));
531,671✔
2973
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
531,671✔
2974
    
2975
    STREAM_CHECK_RET_GOTO(createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream));
531,671✔
2976
  } else {
2977
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2978
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2979
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2980
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2981
  }
2982

2983
  blockDataCleanup(pTaskInner->pResBlockDst);
531,671✔
2984
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
531,446✔
2985
  bool hasNext = true;
531,446✔
2986
  while (true) {
374,107✔
2987
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
905,553✔
2988
    if (!hasNext) {
905,532✔
2989
      break;
531,425✔
2990
    }
2991
    pTaskInner->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
374,107✔
2992
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
374,107✔
2993

2994
    int32_t index = 0;
374,107✔
2995
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.skey));
374,107✔
2996
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.ekey));
374,107✔
2997
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.uid));
374,107✔
2998
    if (!sStreamReaderInfo->isVtableStream) {
374,107✔
2999
      STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.groupId));
105,939✔
3000
    }
3001
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.rows));
374,107✔
3002

3003
    stDebug("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
374,107✔
3004
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
3005
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
3006
            pTaskInner->pResBlockDst->info.rows++;
374,107✔
3007
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
374,107✔
3008
      break;
×
3009
    }
3010
  }
3011

3012
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
531,425✔
3013
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
531,671✔
3014
  printDataBlock(pTaskInner->pResBlockDst, __func__, "meta", ((SStreamTask *)sStreamReaderInfo->pTask)->streamId);
531,671✔
3015
  if (!hasNext) {
531,671✔
3016
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
531,671✔
3017
  }
3018

3019
end:
531,671✔
3020
  STREAM_PRINT_LOG_END_WITHID(code, lino);
531,671✔
3021
  SRpcMsg rsp = {
531,671✔
3022
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3023
  tmsgSendRsp(&rsp);
531,671✔
3024
  taosMemoryFree(pList);
531,671✔
3025
  return code;
531,671✔
3026
}
3027

3028
static int32_t vnodeProcessStreamTsdbTsDataReqNonVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
98,259✔
3029
  int32_t                 code = 0;
98,259✔
3030
  int32_t                 lino = 0;
98,259✔
3031
  SStreamReaderTaskInner* pTaskInner = NULL;
98,259✔
3032
  void*                   buf = NULL;
98,259✔
3033
  size_t                  size = 0;
98,259✔
3034
  SSDataBlock*            pBlockRes = NULL;
98,259✔
3035

3036
  void* pTask = sStreamReaderInfo->pTask;
98,259✔
3037
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
98,259✔
3038
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
3039
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
3040

3041
  int32_t        pNum = 1;
98,259✔
3042
  STableKeyInfo  pList = {.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, req->tsdbTsDataReq.uid), .uid = req->tsdbTsDataReq.uid};
98,259✔
3043
  STREAM_CHECK_CONDITION_GOTO(pList.groupId == -1, TSDB_CODE_INVALID_PARA);
98,259✔
3044
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
98,259✔
3045
               sStreamReaderInfo->triggerCols, false, NULL);
3046
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &pList, pNum, &sStreamReaderInfo->storageApi));
98,259✔
3047
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
98,259✔
3048
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
98,259✔
3049

3050
  while (1) {
98,259✔
3051
    bool hasNext = false;
196,518✔
3052
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
196,518✔
3053
    if (!hasNext) {
196,518✔
3054
      break;
98,259✔
3055
    }
3056
    // if (!sStreamReaderInfo->isVtableStream){
3057
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
98,259✔
3058
    // }
3059

3060
    SSDataBlock* pBlock = NULL;
98,259✔
3061
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
98,259✔
3062
    if (pBlock != NULL && pBlock->info.rows > 0) {
98,259✔
3063
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock,
98,259✔
3064
          0, pBlock->info.rows, 1));
3065
    }
3066
    
3067
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
98,259✔
3068
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
98,259✔
3069
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
98,259✔
3070
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
3071
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
3072
  }
3073

3074
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
98,259✔
3075

3076
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
98,259✔
3077
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
98,259✔
3078

3079
end:
98,259✔
3080
  STREAM_PRINT_LOG_END_WITHID(code, lino);
98,259✔
3081
  SRpcMsg rsp = {
98,259✔
3082
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3083
  tmsgSendRsp(&rsp);
98,259✔
3084
  blockDataDestroy(pBlockRes);
98,259✔
3085

3086
  releaseStreamTask(&pTaskInner);
98,259✔
3087
  return code;
98,259✔
3088
}
3089

3090
static int32_t vnodeProcessStreamTsdbTsDataReqVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
3,388✔
3091
  int32_t                 code = 0;
3,388✔
3092
  int32_t                 lino = 0;
3,388✔
3093
  SStreamReaderTaskInner* pTaskInner = NULL;
3,388✔
3094
  void*                   buf = NULL;
3,388✔
3095
  size_t                  size = 0;
3,388✔
3096
  SSDataBlock*            pBlockRes = NULL;
3,388✔
3097

3098
  void* pTask = sStreamReaderInfo->pTask;
3,388✔
3099
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
3,388✔
3100
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
3101
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
3102

3103
  int32_t        pNum = 1;
3,388✔
3104
  STableKeyInfo  pList = {.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, req->tsdbTsDataReq.uid), .uid = req->tsdbTsDataReq.uid};
3,388✔
3105
  STREAM_CHECK_CONDITION_GOTO(pList.groupId == -1, TSDB_CODE_INVALID_PARA);
3,388✔
3106
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
3,388✔
3107
               sStreamReaderInfo->tsSchemas, true, NULL);
3108
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->tsBlock, &pList, pNum, &sStreamReaderInfo->storageApi));
3,388✔
3109
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
3,388✔
3110

3111
  while (1) {
3,388✔
3112
    bool hasNext = false;
6,776✔
3113
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
6,776✔
3114
    if (!hasNext) {
6,776✔
3115
      break;
3,388✔
3116
    }
3117

3118
    SSDataBlock* pBlock = NULL;
3,388✔
3119
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
3,388✔
3120
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
3,388✔
3121
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
3,388✔
3122
            TD_VID(pVnode), __func__, pBlockRes->info.window.skey, pBlockRes->info.window.ekey,
3123
            pBlockRes->info.id.uid, pBlockRes->info.id.groupId, pBlockRes->info.rows);
3124
  }
3125

3126
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
3,388✔
3127
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
3,388✔
3128

3129
end:
3,388✔
3130
  STREAM_PRINT_LOG_END_WITHID(code, lino);
3,388✔
3131
  SRpcMsg rsp = {
3,388✔
3132
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3133
  tmsgSendRsp(&rsp);
3,388✔
3134
  blockDataDestroy(pBlockRes);
3,388✔
3135

3136
  releaseStreamTask(&pTaskInner);
3,388✔
3137
  return code;
3,388✔
3138
}
3139

3140
static int32_t vnodeProcessStreamTsdbTriggerDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
280,081✔
3141
  int32_t code = 0;
280,081✔
3142
  int32_t lino = 0;
280,081✔
3143
  void*   buf = NULL;
280,081✔
3144
  size_t  size = 0;
280,081✔
3145
  STableKeyInfo* pList = NULL;
280,081✔
3146
  SArray*        pResList = NULL;
280,081✔
3147
  SSDataBlock*   pBlockTmp = NULL;
280,081✔
3148

3149
  SStreamReaderTaskInner* pTaskInner = NULL;
280,081✔
3150
  void* pTask = sStreamReaderInfo->pTask;
280,081✔
3151
  ST_TASK_DLOG("vgId:%d %s start. ver:%"PRId64",order:%d,startTs:%"PRId64",gid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, req->tsdbTriggerDataReq.gid);
280,081✔
3152
  
3153
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
280,081✔
3154

3155
  if (req->base.type == STRIGGER_PULL_TSDB_TRIGGER_DATA) {
280,081✔
3156
    int32_t        pNum = 0;
97,791✔
3157
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbTriggerDataReq.gid, &pList, &pNum));
97,791✔
3158
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, INT64_MAX,
97,791✔
3159
                 sStreamReaderInfo->triggerCols, false, NULL);
3160
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
97,791✔
3161
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
97,791✔
3162
  } else {
3163
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
182,290✔
3164
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
182,290✔
3165
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
58,100✔
3166
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
58,100✔
3167
  }
3168

3169
  blockDataCleanup(pTaskInner->pResBlockDst);
155,891✔
3170
  bool hasNext = true;
155,618✔
3171
  int32_t totalRows = 0;
155,891✔
3172
    
3173
  pResList = taosArrayInit(4, POINTER_BYTES);
155,891✔
3174
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
155,891✔
3175
  while (1) {
150,954✔
3176
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
306,845✔
3177
    if (!hasNext) {
306,845✔
3178
      break;
97,791✔
3179
    }
3180
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
209,054✔
3181
    // pTaskInner->pResBlockDst->info.id.groupId = pTaskInner->pResBlock->info.id.groupId;
3182

3183
    SSDataBlock* pBlock = NULL;
209,054✔
3184
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
209,054✔
3185
    if (pBlock != NULL && pBlock->info.rows > 0) {
209,054✔
3186
      STREAM_CHECK_RET_GOTO(
209,054✔
3187
        processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
3188
    }
3189
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
209,054✔
3190
    // STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
3191
    ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
209,054✔
3192
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pBlock, true, &pBlockTmp));
209,054✔
3193
    STREAM_CHECK_NULL_GOTO(taosArrayPush(pResList, &pBlockTmp), terrno);
209,054✔
3194
    totalRows += blockDataGetNumOfRows(pBlockTmp);
209,054✔
3195
    pBlockTmp = NULL;
209,054✔
3196

3197
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
209,054✔
3198
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
3199
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
3200
    if (totalRows >= STREAM_RETURN_ROWS_NUM) {  //todo optimize send multi blocks in one group
209,054✔
3201
      break;
58,100✔
3202
    }
3203
  }
3204

3205
  STREAM_CHECK_RET_GOTO(buildArrayRsp(pResList, &buf, &size));
155,891✔
3206
  if (!hasNext) {
155,891✔
3207
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
97,791✔
3208
  }
3209

3210
end:
280,081✔
3211
  STREAM_PRINT_LOG_END_WITHID(code, lino);
280,081✔
3212
  SRpcMsg rsp = {
280,081✔
3213
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3214
  tmsgSendRsp(&rsp);
280,081✔
3215
  taosMemoryFree(pList);
279,790✔
3216
  blockDataDestroy(pBlockTmp);
279,790✔
3217
  taosArrayDestroyP(pResList, (FDelete)blockDataDestroy);
279,790✔
3218
  return code;
279,790✔
3219
}
3220

3221
static int32_t vnodeProcessStreamTsdbCalcDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
7,465,415✔
3222
  int32_t code = 0;
7,465,415✔
3223
  int32_t lino = 0;
7,465,415✔
3224
  void*   buf = NULL;
7,465,415✔
3225
  size_t  size = 0;
7,465,415✔
3226
  SSDataBlock*   pBlockRes = NULL;
7,465,415✔
3227
  STableKeyInfo* pList = NULL;
7,465,415✔
3228

3229

3230
  void* pTask = sStreamReaderInfo->pTask;
7,465,415✔
3231
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
7,465,415✔
3232
    req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey, req->tsdbCalcDataReq.gid, req->tsdbCalcDataReq.ver);
3233

3234
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerCols, TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN);
7,465,415✔
3235

3236
  SStreamReaderTaskInner* pTaskInner = NULL;
7,465,415✔
3237
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
7,465,415✔
3238

3239
  if (req->base.type == STRIGGER_PULL_TSDB_CALC_DATA) {
7,465,415✔
3240
    int32_t        pNum = 0;
7,465,415✔
3241
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbCalcDataReq.gid, &pList, &pNum));
7,465,415✔
3242
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbCalcDataReq.ver, TSDB_ORDER_ASC, req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey,
7,465,415✔
3243
                 sStreamReaderInfo->triggerCols, false, NULL);
3244
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
7,465,172✔
3245

3246
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
7,458,199✔
3247
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
7,464,218✔
3248
  } else {
3249
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
3250
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
3251
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
3252
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
3253
  }
3254

3255
  blockDataCleanup(pTaskInner->pResBlockDst);
7,461,322✔
3256
  bool hasNext = true;
7,462,292✔
3257
  while (1) {
962,158✔
3258
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
8,424,432✔
3259
    if (!hasNext) {
8,419,114✔
3260
      break;
7,457,920✔
3261
    }
3262
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
961,194✔
3263

3264
    SSDataBlock* pBlock = NULL;
961,708✔
3265
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
962,158✔
3266
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
961,033✔
3267
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
961,258✔
3268
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
961,708✔
3269
      break;
×
3270
    }
3271
  }
3272

3273
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
7,457,920✔
3274
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
7,463,957✔
3275
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
7,459,828✔
3276
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
7,462,499✔
3277
  printDataBlock(pBlockRes, __func__, "tsdb_calc_data", ((SStreamTask*)pTask)->streamId);
7,459,117✔
3278
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
7,458,585✔
3279
  printDataBlock(pBlockRes, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
7,464,929✔
3280

3281
  if (!hasNext) {
7,465,415✔
3282
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
7,465,415✔
3283
  }
3284

3285
end:
7,467,078✔
3286
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,463,957✔
3287
  SRpcMsg rsp = {
7,465,415✔
3288
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3289
  tmsgSendRsp(&rsp);
7,465,415✔
3290
  blockDataDestroy(pBlockRes);
7,465,172✔
3291
  taosMemoryFree(pList);
7,464,929✔
3292
  return code;
7,465,415✔
3293
}
3294

3295
static int32_t vnodeProcessStreamTsdbVirtalDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
676,238✔
3296
  int32_t code = 0;
676,238✔
3297
  int32_t lino = 0;
676,238✔
3298
  void*   buf = NULL;
676,238✔
3299
  size_t  size = 0;
676,238✔
3300
  int32_t* slotIdList = NULL;
676,238✔
3301
  SArray* sortedCid = NULL;
676,238✔
3302
  SArray* schemas = NULL;
676,238✔
3303
  SSDataBlock*   pBlockRes = NULL;
676,238✔
3304
  
3305
  void* pTask = sStreamReaderInfo->pTask;
676,238✔
3306
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
676,238✔
3307
    req->tsdbDataReq.skey, req->tsdbDataReq.ekey, req->tsdbDataReq.uid, req->tsdbDataReq.ver);
3308
    
3309
  SStreamReaderTaskInner* pTaskInner = NULL;
676,238✔
3310
  int64_t key = req->tsdbDataReq.uid;
676,238✔
3311

3312
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
676,238✔
3313
    // sort cid and build slotIdList
3314
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
676,238✔
3315
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
676,238✔
3316
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
676,238✔
3317
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
676,238✔
3318
    taosArraySort(sortedCid, sortCid);
676,238✔
3319
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
2,652,991✔
3320
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
1,977,600✔
3321
      STREAM_CHECK_NULL_GOTO(cid, terrno);
1,977,600✔
3322
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
3,911,951✔
3323
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
3,911,951✔
3324
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
3,911,104✔
3325
        if (*cid == *cidSorted) {
3,911,104✔
3326
          slotIdList[j] = i;
1,976,753✔
3327
          break;
1,976,753✔
3328
        }
3329
      }
3330
    }
3331

3332
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, req->tsdbDataReq.uid, &schemas, &sStreamReaderInfo->storageApi));
675,391✔
3333
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
672,850✔
3334
    STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlockRes));
672,850✔
3335

3336
    taosArraySort(schemas, sortSSchema);
672,850✔
3337
    BUILD_OPTION(options, req->tsdbDataReq.suid, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
672,850✔
3338
                    req->tsdbDataReq.ekey, schemas, true, &slotIdList);
3339
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid, .groupId = 0};
672,850✔
3340
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, pBlockRes, &keyInfo, 1, &sStreamReaderInfo->storageApi));
672,850✔
3341
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
672,850✔
3342
    pTaskInner->pResBlockDst = pBlockRes;
672,850✔
3343
    pBlockRes = NULL;
672,850✔
3344
  } else {
3345
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
3346
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
3347
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
3348
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
3349
  }
3350

3351
  blockDataCleanup(pTaskInner->pResBlockDst);
672,850✔
3352
  bool hasNext = true;
672,850✔
3353
  while (1) {
672,850✔
3354
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,345,700✔
3355
    if (!hasNext) {
1,345,700✔
3356
      break;
672,850✔
3357
    }
3358

3359
    SSDataBlock* pBlock = NULL;
672,850✔
3360
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
672,850✔
3361
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
672,850✔
3362
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
672,850✔
3363
      break;
×
3364
    }
3365
  }
3366
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
672,850✔
3367
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
672,850✔
3368
  printDataBlock(pTaskInner->pResBlockDst, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
672,850✔
3369
  if (!hasNext) {
672,850✔
3370
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
672,850✔
3371
  }
3372

3373
end:
676,238✔
3374
  STREAM_PRINT_LOG_END_WITHID(code, lino);
676,238✔
3375
  SRpcMsg rsp = {
676,238✔
3376
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3377
  tmsgSendRsp(&rsp);
676,238✔
3378
  taosMemFree(slotIdList);
676,238✔
3379
  taosArrayDestroy(sortedCid);
676,238✔
3380
  taosArrayDestroy(schemas);
676,238✔
3381
  blockDataDestroy(pBlockRes);
676,238✔
3382
  return code;
676,238✔
3383
}
3384

3385
static int32_t vnodeProcessStreamWalMetaNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
16,868,437✔
3386
  int32_t      code = 0;
16,868,437✔
3387
  int32_t      lino = 0;
16,868,437✔
3388
  void*        buf = NULL;
16,868,437✔
3389
  size_t       size = 0;
16,868,437✔
3390
  int64_t      lastVer = 0;
16,868,437✔
3391
  SSTriggerWalNewRsp resultRsp = {0};
16,868,437✔
3392

3393
  void* pTask = sStreamReaderInfo->pTask;
16,869,797✔
3394
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaNewReq.lastVer);
16,869,543✔
3395

3396
  if (sStreamReaderInfo->metaBlock == NULL) {
16,870,022✔
3397
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
226,366✔
3398
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
226,366✔
3399
  }
3400
  blockDataEmpty(sStreamReaderInfo->metaBlock);
16,869,797✔
3401
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
16,868,048✔
3402
  resultRsp.ver = req->walMetaNewReq.lastVer;
16,868,321✔
3403
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
16,868,321✔
3404

3405
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
16,869,524✔
3406
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
16,867,830✔
3407
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
590,974✔
3408
  buf = rpcMallocCont(size);
590,974✔
3409
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
590,974✔
3410
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
590,974✔
3411
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
590,476✔
3412
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
590,974✔
3413

3414
end:
16,867,830✔
3415
  if (code == 0 && resultRsp.totalRows == 0) {
16,868,677✔
3416
    code = TSDB_CODE_STREAM_NO_DATA;
16,277,976✔
3417
    size = sizeof(int64_t) * 2;
16,277,976✔
3418
    buf = rpcMallocCont(size);
16,277,976✔
3419
    *(int64_t*)buf = resultRsp.ver;
16,276,533✔
3420
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
16,276,533✔
3421
  }
3422
  SRpcMsg rsp = {
16,868,020✔
3423
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3424
  tmsgSendRsp(&rsp);
16,867,849✔
3425
  if (code == TSDB_CODE_STREAM_NO_DATA){
16,869,582✔
3426
    code = 0;
16,278,083✔
3427
  }
3428
  STREAM_PRINT_LOG_END_WITHID(code, lino);
16,869,582✔
3429
  blockDataDestroy(resultRsp.deleteBlock);
16,869,816✔
3430
  blockDataDestroy(resultRsp.tableBlock);
16,869,062✔
3431

3432
  return code;
16,869,316✔
3433
}
3434
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
5,866,180✔
3435
  int32_t      code = 0;
5,866,180✔
3436
  int32_t      lino = 0;
5,866,180✔
3437
  void*        buf = NULL;
5,866,180✔
3438
  size_t       size = 0;
5,866,180✔
3439
  SSTriggerWalNewRsp resultRsp = {0};
5,866,180✔
3440
  
3441
  void* pTask = sStreamReaderInfo->pTask;
5,867,269✔
3442
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaDataNewReq.lastVer);
5,866,726✔
3443

3444
  if (sStreamReaderInfo->metaBlock == NULL) {
5,866,726✔
3445
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
145,624✔
3446
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
145,624✔
3447
  }
3448

3449
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
5,867,813✔
3450
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
5,866,724✔
3451
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
5,865,266✔
3452
  resultRsp.checkAlter = true;
5,867,731✔
3453
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
5,867,731✔
3454
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
5,862,278✔
3455

3456
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp));
5,862,278✔
3457

3458
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
5,863,608✔
3459
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
177,863✔
3460
  buf = rpcMallocCont(size);
179,209✔
3461
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
179,515✔
3462
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
179,515✔
3463
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
179,515✔
3464
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
179,515✔
3465
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
179,515✔
3466
  printIndexHash(resultRsp.indexHash, pTask);
179,515✔
3467

3468
end:
5,865,502✔
3469
  if (resultRsp.totalRows == 0) {
5,864,411✔
3470
    code = TSDB_CODE_STREAM_NO_DATA;
5,688,059✔
3471
    size = sizeof(int64_t) * 2;
5,688,059✔
3472
    buf = rpcMallocCont(size);
5,688,059✔
3473
    *(int64_t*)buf = resultRsp.ver;
5,683,275✔
3474
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
5,683,547✔
3475
  }
3476
  SRpcMsg rsp = {
5,859,627✔
3477
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3478
  tmsgSendRsp(&rsp);
5,862,657✔
3479
  if (code == TSDB_CODE_STREAM_NO_DATA){
5,866,372✔
3480
    code = 0;
5,687,608✔
3481
  }
3482
  blockDataDestroy(resultRsp.dataBlock);
5,866,372✔
3483
  blockDataDestroy(resultRsp.deleteBlock);
5,865,303✔
3484
  blockDataDestroy(resultRsp.tableBlock);
5,866,487✔
3485
  tSimpleHashCleanup(resultRsp.indexHash);
5,867,033✔
3486

3487
  STREAM_PRINT_LOG_END_WITHID(code, lino);
5,859,135✔
3488

3489
  return code;
5,862,636✔
3490
}
3491

3492
static int32_t vnodeProcessStreamWalDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
11,128,937✔
3493
  int32_t      code = 0;
11,128,937✔
3494
  int32_t      lino = 0;
11,128,937✔
3495
  void*        buf = NULL;
11,128,937✔
3496
  size_t       size = 0;
11,128,937✔
3497
  SSTriggerWalNewRsp resultRsp = {0};
11,128,937✔
3498

3499
  void* pTask = sStreamReaderInfo->pTask;
11,128,937✔
3500
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
11,128,937✔
3501

3502
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
11,128,937✔
3503
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
11,126,697✔
3504
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
11,125,276✔
3505
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
11,125,276✔
3506
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
11,124,702✔
3507

3508
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
11,124,702✔
3509
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
11,129,210✔
3510

3511
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
11,128,664✔
3512

3513
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
387,773✔
3514
  buf = rpcMallocCont(size);
387,773✔
3515
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
387,773✔
3516
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
387,773✔
3517
  printIndexHash(resultRsp.indexHash, pTask);
387,773✔
3518

3519
end:
11,128,664✔
3520
  if (resultRsp.totalRows == 0) {
11,128,664✔
3521
    buf = rpcMallocCont(sizeof(int64_t));
10,741,164✔
3522
    *(int64_t *)buf = resultRsp.ver;
10,740,347✔
3523
    size = sizeof(int64_t);
10,740,620✔
3524
    code = TSDB_CODE_STREAM_NO_DATA;
10,740,620✔
3525
  }
3526
  SRpcMsg rsp = {
11,128,120✔
3527
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3528
  tmsgSendRsp(&rsp);
11,129,210✔
3529
  if (code == TSDB_CODE_STREAM_NO_DATA){
11,127,563✔
3530
    code = 0;
10,740,637✔
3531
  }
3532

3533
  blockDataDestroy(resultRsp.dataBlock);
11,127,563✔
3534
  blockDataDestroy(resultRsp.deleteBlock);
11,127,305✔
3535
  blockDataDestroy(resultRsp.tableBlock);
11,126,698✔
3536
  tSimpleHashCleanup(resultRsp.indexHash);
11,127,818✔
3537
  tSimpleHashCleanup(resultRsp.uidHash);
11,123,960✔
3538
  STREAM_PRINT_LOG_END_WITHID(code, lino);
11,125,899✔
3539

3540
  return code;
11,128,680✔
3541
}
3542

3543
static int32_t vnodeProcessStreamWalCalcDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,170,270✔
3544
  int32_t      code = 0;
1,170,270✔
3545
  int32_t      lino = 0;
1,170,270✔
3546
  void*        buf = NULL;
1,170,270✔
3547
  size_t       size = 0;
1,170,270✔
3548
  SSTriggerWalNewRsp resultRsp = {0};
1,170,270✔
3549
  SSDataBlock* pBlock1 = NULL;
1,170,270✔
3550
  SSDataBlock* pBlock2 = NULL;
1,170,270✔
3551
  
3552
  void* pTask = sStreamReaderInfo->pTask;
1,170,270✔
3553
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
1,170,270✔
3554

3555
  SSDataBlock* dataBlock = sStreamReaderInfo->isVtableStream ? sStreamReaderInfo->calcBlock : sStreamReaderInfo->triggerBlock;
1,170,270✔
3556
  STREAM_CHECK_RET_GOTO(createOneDataBlock(dataBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
1,170,270✔
3557
  resultRsp.isCalc = sStreamReaderInfo->isVtableStream ? true : false;
1,169,997✔
3558
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,169,997✔
3559
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
1,170,270✔
3560
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,170,270✔
3561
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
1,170,270✔
3562

3563
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
1,170,270✔
3564
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
1,170,270✔
3565

3566
  if (!sStreamReaderInfo->isVtableStream){
301,863✔
3567
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
193,747✔
3568
  
3569
    blockDataTransform(pBlock2, resultRsp.dataBlock);
193,747✔
3570
    blockDataDestroy(resultRsp.dataBlock);
193,747✔
3571
    resultRsp.dataBlock = pBlock2;
193,475✔
3572
    pBlock2 = NULL;
193,475✔
3573
  }
3574

3575
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
301,591✔
3576
  buf = rpcMallocCont(size);
301,319✔
3577
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
301,863✔
3578
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
301,863✔
3579
  printIndexHash(resultRsp.indexHash, pTask);
301,863✔
3580

3581
end:
1,170,270✔
3582
  if (resultRsp.totalRows == 0) {
1,170,270✔
3583
    buf = rpcMallocCont(sizeof(int64_t));
868,407✔
3584
    *(int64_t *)buf = resultRsp.ver;
868,407✔
3585
    size = sizeof(int64_t);
868,407✔
3586
    code = TSDB_CODE_STREAM_NO_DATA;
868,407✔
3587
  }
3588
  SRpcMsg rsp = {
1,170,270✔
3589
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3590
  tmsgSendRsp(&rsp);
1,170,270✔
3591
  if (code == TSDB_CODE_STREAM_NO_DATA){
1,170,270✔
3592
    code = 0;
868,407✔
3593
  }
3594

3595
  blockDataDestroy(pBlock1);
1,170,270✔
3596
  blockDataDestroy(pBlock2);
1,170,270✔
3597
  blockDataDestroy(resultRsp.dataBlock);
1,170,270✔
3598
  blockDataDestroy(resultRsp.deleteBlock);
1,170,270✔
3599
  blockDataDestroy(resultRsp.tableBlock);
1,170,270✔
3600
  tSimpleHashCleanup(resultRsp.indexHash);
1,170,270✔
3601
  tSimpleHashCleanup(resultRsp.uidHash);
1,170,270✔
3602
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,170,270✔
3603

3604
  return code;
1,170,270✔
3605
}
3606

3607
static int32_t vnodeProcessStreamGroupColValueReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
371,807✔
3608
  int32_t code = 0;
371,807✔
3609
  int32_t lino = 0;
371,807✔
3610
  void*   buf = NULL;
371,807✔
3611
  size_t  size = 0;
371,807✔
3612
  SArray** gInfo = NULL;
371,807✔
3613
  
3614
  void* pTask = sStreamReaderInfo->pTask;
371,807✔
3615
  ST_TASK_DLOG("vgId:%d %s start, request gid:%" PRId64, TD_VID(pVnode), __func__, req->groupColValueReq.gid);
371,807✔
3616

3617
  gInfo = taosHashAcquire(sStreamReaderInfo->groupIdMap, &req->groupColValueReq.gid, POINTER_BYTES);
371,807✔
3618
  STREAM_CHECK_NULL_GOTO(gInfo, TSDB_CODE_STREAM_NO_CONTEXT);
371,807✔
3619
  SStreamGroupInfo pGroupInfo = {0};
371,807✔
3620
  pGroupInfo.gInfo = *gInfo;
371,807✔
3621

3622
  size = tSerializeSStreamGroupInfo(NULL, 0, &pGroupInfo, TD_VID(pVnode));
371,807✔
3623
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
3624
  buf = rpcMallocCont(size);
371,551✔
3625
  STREAM_CHECK_NULL_GOTO(buf, terrno);
371,807✔
3626
  size = tSerializeSStreamGroupInfo(buf, size, &pGroupInfo, TD_VID(pVnode));
371,807✔
3627
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
3628
end:
371,807✔
3629
  taosHashRelease(sStreamReaderInfo->groupIdMap, gInfo);
371,807✔
3630
  if (code != 0) {
371,807✔
3631
    rpcFreeCont(buf);
×
3632
    buf = NULL;
×
3633
    size = 0;
×
3634
  }
3635
  STREAM_PRINT_LOG_END_WITHID(code, lino);
371,807✔
3636
  SRpcMsg rsp = {
371,807✔
3637
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3638
  tmsgSendRsp(&rsp);
371,807✔
3639

3640
  return code;
371,807✔
3641
}
3642

3643
static int32_t setVtableInfo(SVnode* pVnode, SArray* infos, SArray* cids, int64_t uid, uint64_t gid, int64_t ver, SMetaReader* metaReader, SStreamTriggerReaderInfo* sStreamReaderInfo) {
208,167✔
3644
  int32_t              code = 0;
208,167✔
3645
  int32_t              lino = 0;
208,167✔
3646
  void* pTask = sStreamReaderInfo->pTask;
208,167✔
3647

3648
  VTableInfo* vTable = taosArrayReserve(infos, 1);
208,167✔
3649
  STREAM_CHECK_NULL_GOTO(vTable, terrno);
208,167✔
3650
  vTable->uid = uid;
208,167✔
3651
  vTable->gId = gid;
208,167✔
3652

3653
  ST_TASK_DLOG("vgId:%d %s put vtable uid:%"PRId64, TD_VID(pVnode), __func__, uid);
208,167✔
3654

3655
  code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(metaReader, ver, uid);
208,167✔
3656
  if (code != 0) {
208,167✔
3657
    ST_TASK_ELOG("vgId:%d %s get table entry by uid:%"PRId64" failed, msg:%s", TD_VID(pVnode), __func__, uid, tstrerror(code));
×
3658
    goto end;
×
3659
  }
3660
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 1) {
208,167✔
3661
    vTable->cols.nCols = metaReader->me.colRef.nCols;
11,979✔
3662
    vTable->cols.version = metaReader->me.colRef.version;
11,979✔
3663
    vTable->cols.pColRef = taosMemoryCalloc(metaReader->me.colRef.nCols, sizeof(SColRef));
11,979✔
3664
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
11,979✔
3665
    for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
71,874✔
3666
      memcpy(vTable->cols.pColRef + j, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
59,895✔
3667
    }
3668
  } else {
3669
    vTable->cols.nCols = taosArrayGetSize(cids);
196,188✔
3670
    vTable->cols.version = metaReader->me.colRef.version;
196,188✔
3671
    vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
196,188✔
3672
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
196,188✔
3673
    for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
745,502✔
3674
      for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
2,316,223✔
3675
        if (metaReader->me.colRef.pColRef[j].hasRef &&
2,119,219✔
3676
            metaReader->me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
1,564,786✔
3677
          memcpy(vTable->cols.pColRef + i, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
352,310✔
3678
          break;
352,310✔
3679
        }
3680
      }
3681
    }
3682
  }
3683
  tDecoderClear(&metaReader->coder);
208,167✔
3684

3685
end:
208,167✔
3686
  return code;
208,167✔
3687
}
3688

3689
static int32_t getAllVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SArray* cids, int64_t ver, SMetaReader* metaReader, SStreamTriggerReaderInfo* sStreamReaderInfo){
87,716✔
3690
  int32_t              code = 0;
87,716✔
3691
  int32_t              lino = 0;
87,716✔
3692
  void* pTask = sStreamReaderInfo->pTask;
87,716✔
3693
  SArray*              pTableListArray = NULL;
87,716✔
3694

3695

3696
  pTableListArray = qStreamGetTableArrayList(sStreamReaderInfo);
87,716✔
3697
  STREAM_CHECK_NULL_GOTO(pTableListArray, terrno);
87,716✔
3698

3699
  vTableInfo->infos = taosArrayInit(taosArrayGetSize(pTableListArray), sizeof(VTableInfo));
87,716✔
3700
  STREAM_CHECK_NULL_GOTO(vTableInfo->infos, terrno);
87,716✔
3701

3702
  for (size_t i = 0; i < taosArrayGetSize(pTableListArray); i++) {
295,883✔
3703
    SStreamTableKeyInfo* pKeyInfo = taosArrayGetP(pTableListArray, i);
208,167✔
3704
    if (pKeyInfo == NULL || pKeyInfo->markedDeleted) {
208,167✔
3705
      continue;
×
3706
    }
3707
    code = setVtableInfo(pVnode, vTableInfo->infos, cids, pKeyInfo->uid, pKeyInfo->groupId, ver, metaReader, sStreamReaderInfo);
208,167✔
3708
    if (code != 0) {
208,167✔
3709
      ST_TASK_WLOG("vgId:%d %s set vtable info uid:%"PRId64" failed, msg:%s", TD_VID(pVnode), __func__, pKeyInfo->uid, tstrerror(code));
×
3710
      code = 0;
×
3711
      continue;
×
3712
    }
3713
  }
3714

3715
end:
87,716✔
3716
  taosArrayDestroyP(pTableListArray, taosMemFree);
87,716✔
3717
  return code;
87,716✔
3718
}
3719

3720
static int32_t getSpicificVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SArray* uids, SArray* cids, int64_t ver, SMetaReader* metaReader, SStreamTriggerReaderInfo* sStreamReaderInfo){
×
3721
  int32_t              code = 0;
×
3722
  int32_t              lino = 0;
×
3723
  void* pTask = sStreamReaderInfo->pTask;
×
3724

3725
  vTableInfo->infos = taosArrayInit(taosArrayGetSize(uids), sizeof(VTableInfo));
×
3726
  STREAM_CHECK_NULL_GOTO(vTableInfo->infos, terrno);
×
3727

3728
  for (size_t i = 0; i < taosArrayGetSize(uids); i++) {
×
3729
    int64_t* uid = taosArrayGet(uids, i);
×
3730
    STREAM_CHECK_NULL_GOTO(uid, terrno);
×
3731

3732
    taosRLockLatch(&sStreamReaderInfo->lock);
×
3733
    uint64_t groupId = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, *uid);
×
3734
    taosRUnLockLatch(&sStreamReaderInfo->lock);
×
3735
    if (groupId == -1) {
×
3736
      ST_TASK_WLOG("vgId:%d %s uid:%"PRId64" not found in stream group", TD_VID(pVnode), __func__, *uid);
×
3737
      continue;
×
3738
    }
3739
    code = setVtableInfo(pVnode, vTableInfo->infos, cids, *uid, groupId, ver, metaReader, sStreamReaderInfo);
×
3740
    if (code != 0) {
×
3741
      ST_TASK_WLOG("vgId:%d %s set vtable info uid:%"PRId64" failed, msg:%s", TD_VID(pVnode), __func__, *uid, tstrerror(code));
×
3742
      code = 0;
×
3743
      continue;
×
3744
    }
3745
  }
3746
  
3747
end:
×
3748
  return code;
×
3749
}
3750

3751
static int32_t vnodeProcessStreamVTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
87,716✔
3752
  int32_t              code = 0;
87,716✔
3753
  int32_t              lino = 0;
87,716✔
3754
  void*                buf = NULL;
87,716✔
3755
  size_t               size = 0;
87,716✔
3756
  SStreamMsgVTableInfo vTableInfo = {0};
87,716✔
3757
  SMetaReader          metaReader = {0};
87,716✔
3758

3759
  void* pTask = sStreamReaderInfo->pTask;
87,716✔
3760
  ST_TASK_DLOG("vgId:%d %s start, version:%"PRId64, TD_VID(pVnode), __func__, req->virTableInfoReq.ver);
87,716✔
3761

3762
  SArray* cids = req->virTableInfoReq.cids;
87,716✔
3763
  STREAM_CHECK_NULL_GOTO(cids, terrno);
87,716✔
3764

3765
  if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
87,716✔
3766
    (void)atomic_val_compare_exchange_8(&sStreamReaderInfo->isVtableOnlyTs, 0, 1);
11,979✔
3767
  }
3768
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn);
87,716✔
3769

3770
  if (req->virTableInfoReq.fetchAllTable || req->virTableInfoReq.uids == NULL || taosArrayGetSize(req->virTableInfoReq.uids) == 0) {
87,716✔
3771
    STREAM_CHECK_RET_GOTO(getAllVinfo(pVnode, &vTableInfo, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
87,716✔
3772
  } else {
3773
    STREAM_CHECK_RET_GOTO(getSpicificVinfo(pVnode, &vTableInfo, req->virTableInfoReq.uids, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
×
3774
  }
3775
  ST_TASK_DLOG("vgId:%d %s end, size:%"PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(vTableInfo.infos));
87,716✔
3776
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
87,716✔
3777

3778
end:
87,716✔
3779
  tDestroySStreamMsgVTableInfo(&vTableInfo);
87,716✔
3780
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
87,716✔
3781
  STREAM_PRINT_LOG_END_WITHID(code, lino);
87,716✔
3782
  SRpcMsg rsp = {
87,716✔
3783
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3784
  tmsgSendRsp(&rsp);
87,716✔
3785
  return code;
87,716✔
3786
}
3787

3788
static int32_t vnodeProcessStreamOTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
149,650✔
3789
  int32_t                   code = 0;
149,650✔
3790
  int32_t                   lino = 0;
149,650✔
3791
  void*                     buf = NULL;
149,650✔
3792
  size_t                    size = 0;
149,650✔
3793
  SSTriggerOrigTableInfoRsp oTableInfo = {0};
149,650✔
3794
  SMetaReader               metaReader = {0};
149,650✔
3795
  void*                     pTask = sStreamReaderInfo->pTask;
149,650✔
3796

3797
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64, TD_VID(pVnode), __func__, req->origTableInfoReq.ver);
149,650✔
3798

3799
  SArray* cols = req->origTableInfoReq.cols;
149,650✔
3800
  STREAM_CHECK_NULL_GOTO(cols, terrno);
149,650✔
3801

3802
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
149,650✔
3803

3804
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
149,650✔
3805

3806
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn);
149,650✔
3807
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
549,876✔
3808
    OTableInfo*    oInfo = taosArrayGet(cols, i);
400,226✔
3809
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
400,226✔
3810
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
400,226✔
3811
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
400,226✔
3812
    code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionName(&metaReader, req->origTableInfoReq.ver, oInfo->refTableName);
400,226✔
3813
    if (code != 0) {
400,226✔
3814
      code = 0;
242✔
3815
      ST_TASK_ELOG("vgId:%d %s get table entry by name:%s failed, msg:%s", TD_VID(pVnode), __func__, oInfo->refTableName, tstrerror(code));
242✔
3816
      continue;
242✔
3817
    }
3818
    vTableInfo->uid = metaReader.me.uid;
399,984✔
3819
    ST_TASK_DLOG("vgId:%d %s get original uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
399,984✔
3820

3821
    SSchemaWrapper* sSchemaWrapper = NULL;
399,984✔
3822
    if (metaReader.me.type == TD_CHILD_TABLE) {
399,984✔
3823
      int64_t suid = metaReader.me.ctbEntry.suid;
397,819✔
3824
      vTableInfo->suid = suid;
397,819✔
3825
      tDecoderClear(&metaReader.coder);
397,819✔
3826
      STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->origTableInfoReq.ver, suid));
397,819✔
3827
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
397,819✔
3828
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
2,165✔
3829
      vTableInfo->suid = 0;
2,165✔
3830
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
2,165✔
3831
    } else {
3832
      ST_TASK_ELOG("invalid table type:%d", metaReader.me.type);
×
3833
    }
3834

3835
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
1,374,226✔
3836
      SSchema* s = sSchemaWrapper->pSchema + j;
1,374,226✔
3837
      if (strcmp(s->name, oInfo->refColName) == 0) {
1,374,226✔
3838
        vTableInfo->cid = s->colId;
399,984✔
3839
        break;
399,984✔
3840
      }
3841
    }
3842
    if (vTableInfo->cid == 0) {
399,984✔
3843
      stError("vgId:%d %s, not found col %s in table %s", TD_VID(pVnode), __func__, oInfo->refColName,
×
3844
              oInfo->refTableName);
3845
    }
3846
    tDecoderClear(&metaReader.coder);
399,984✔
3847
  }
3848

3849
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
149,650✔
3850

3851
end:
149,650✔
3852
  tDestroySTriggerOrigTableInfoRsp(&oTableInfo);
149,650✔
3853
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
149,650✔
3854
  STREAM_PRINT_LOG_END_WITHID(code, lino);
149,650✔
3855
  SRpcMsg rsp = {
149,650✔
3856
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3857
  tmsgSendRsp(&rsp);
149,650✔
3858
  return code;
149,650✔
3859
}
3860

3861
static int32_t vnodeProcessStreamVTableTagInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,314,090✔
3862
  int32_t                   code = 0;
1,314,090✔
3863
  int32_t                   lino = 0;
1,314,090✔
3864
  void*                     buf = NULL;
1,314,090✔
3865
  size_t                    size = 0;
1,314,090✔
3866
  SSDataBlock* pBlock = NULL;
1,314,090✔
3867

3868
  SMetaReader               metaReader = {0};
1,314,090✔
3869
  SMetaReader               metaReaderStable = {0};
1,314,090✔
3870
  int64_t streamId = req->base.streamId;
1,314,090✔
3871
  stsDebug("vgId:%d %s start, ver:%"PRId64, TD_VID(pVnode), __func__, req->virTablePseudoColReq.ver);
1,314,090✔
3872

3873
  SArray* cols = req->virTablePseudoColReq.cids;
1,314,090✔
3874
  STREAM_CHECK_NULL_GOTO(cols, terrno);
1,314,090✔
3875

3876
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn);
1,314,090✔
3877
  STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid));
1,314,090✔
3878

3879
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
1,314,090✔
3880

3881
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
1,314,090✔
3882
  if (metaReader.me.type == TD_VIRTUAL_NORMAL_TABLE) {
1,314,090✔
3883
    STREAM_CHECK_CONDITION_GOTO (taosArrayGetSize(cols) < 1 || *(col_id_t*)taosArrayGet(cols, 0) != -1, TSDB_CODE_INVALID_PARA);
2,455✔
3884
    SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
2,455✔
3885
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
2,455✔
3886
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
2,455✔
3887
    pBlock->info.rows = 1;
2,455✔
3888
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
2,455✔
3889
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
2,455✔
3890
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
2,455✔
3891
  } else if (metaReader.me.type == TD_VIRTUAL_CHILD_TABLE){
1,311,635✔
3892
    int64_t suid = metaReader.me.ctbEntry.suid;
1,311,635✔
3893
    sStreamReaderInfo->storageApi.metaReaderFn.readerReleaseLock(&metaReader);
1,311,635✔
3894
    sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReaderStable, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn);
1,311,635✔
3895

3896
    STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReaderStable, req->virTablePseudoColReq.ver, suid));
1,311,635✔
3897
    SSchemaWrapper*  sSchemaWrapper = &metaReaderStable.me.stbEntry.schemaTag;
1,311,635✔
3898
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
3,437,130✔
3899
      col_id_t* id = taosArrayGet(cols, i);
2,125,495✔
3900
      STREAM_CHECK_NULL_GOTO(id, terrno);
2,125,495✔
3901
      if (*id == -1) {
2,125,495✔
3902
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
1,305,706✔
3903
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
1,305,706✔
3904
        continue;
1,305,706✔
3905
      }
3906
      size_t j = 0;
819,789✔
3907
      for (; j < sSchemaWrapper->nCols; j++) {
1,594,363✔
3908
        SSchema* s = sSchemaWrapper->pSchema + j;
1,594,363✔
3909
        if (s->colId == *id) {
1,594,363✔
3910
          SColumnInfoData idata = createColumnInfoData(s->type, s->bytes, s->colId);
819,789✔
3911
          STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
819,789✔
3912
          break;
819,789✔
3913
        }
3914
      }
3915
      if (j == sSchemaWrapper->nCols) {
819,789✔
3916
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, *id);
×
3917
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
3918
      }
3919
    }
3920
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
1,311,635✔
3921
    pBlock->info.rows = 1;
1,310,788✔
3922
    
3923
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
3,436,283✔
3924
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
2,124,648✔
3925
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
2,124,648✔
3926

3927
      if (pDst->info.colId == -1) {
2,124,648✔
3928
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
1,305,706✔
3929
        continue;
1,305,706✔
3930
      }
3931
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
819,789✔
3932
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
3933
        continue;
×
3934
      }
3935

3936
      STagVal val = {0};
819,789✔
3937
      val.cid = pDst->info.colId;
819,789✔
3938
      const char* p = sStreamReaderInfo->storageApi.metaFn.extractTagVal(metaReader.me.ctbEntry.pTags, pDst->info.type, &val);
819,789✔
3939

3940
      char* data = NULL;
819,789✔
3941
      if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
819,789✔
3942
        data = tTagValToData((const STagVal*)p, false);
819,789✔
3943
      } else {
3944
        data = (char*)p;
×
3945
      }
3946

3947
      STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data,
819,789✔
3948
                            (data == NULL) || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data))));
3949

3950
      if ((pDst->info.type != TSDB_DATA_TYPE_JSON) && (p != NULL) && IS_VAR_DATA_TYPE(((const STagVal*)p)->type) &&
819,789✔
3951
          (data != NULL)) {
3952
        taosMemoryFree(data);
768,224✔
3953
      }
3954
    }
3955
  } else {
3956
    stError("vgId:%d %s, invalid table type:%d", TD_VID(pVnode), __func__, metaReader.me.type);
×
3957
    code = TSDB_CODE_INVALID_PARA;
×
3958
    goto end;
×
3959
  }
3960
  
3961
  stsDebug("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
1,314,090✔
3962
  printDataBlock(pBlock, __func__, "", streamId);
1,314,090✔
3963
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
1,314,090✔
3964

3965
end:
1,314,090✔
3966
  if(size == 0){
1,314,090✔
3967
    code = TSDB_CODE_STREAM_NO_DATA;
×
3968
  }
3969
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReaderStable);
1,314,090✔
3970
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
1,314,090✔
3971
  STREAM_PRINT_LOG_END(code, lino);
1,314,090✔
3972
  SRpcMsg rsp = {
1,314,090✔
3973
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3974
  tmsgSendRsp(&rsp);
1,314,090✔
3975
  blockDataDestroy(pBlock);
1,314,090✔
3976
  return code;
1,314,090✔
3977
}
3978

3979
static int32_t vnodeProcessStreamFetchMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo *pInfo) {
8,548,781✔
3980
  int32_t            code = 0;
8,548,781✔
3981
  int32_t            lino = 0;
8,548,781✔
3982
  void*              buf = NULL;
8,548,781✔
3983
  size_t             size = 0;
8,548,781✔
3984
  void*              taskAddr = NULL;
8,548,781✔
3985
  SArray*            pResList = NULL;
8,548,781✔
3986
  bool               hasNext = false;
8,548,781✔
3987
  SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo = NULL;
8,548,781✔
3988

3989
  SResFetchReq req = {0};
8,548,781✔
3990
  STREAM_CHECK_CONDITION_GOTO(tDeserializeSResFetchReq(pMsg->pCont, pMsg->contLen, &req) < 0,
8,548,781✔
3991
                              TSDB_CODE_QRY_INVALID_INPUT);
3992
  SArray* calcInfoList = (SArray*)qStreamGetReaderInfo(req.queryId, req.taskId, &taskAddr);
8,548,471✔
3993
  STREAM_CHECK_NULL_GOTO(calcInfoList, terrno);
8,548,781✔
3994

3995
  STREAM_CHECK_CONDITION_GOTO(req.execId < 0, TSDB_CODE_INVALID_PARA);
8,548,781✔
3996
  sStreamReaderCalcInfo = taosArrayGetP(calcInfoList, req.execId);
8,548,781✔
3997
  STREAM_CHECK_NULL_GOTO(sStreamReaderCalcInfo, terrno);
8,548,781✔
3998
  sStreamReaderCalcInfo->rtInfo.execId = req.execId;
8,548,781✔
3999

4000
  void* pTask = sStreamReaderCalcInfo->pTask;
8,548,781✔
4001
  ST_TASK_DLOG("vgId:%d %s start, execId:%d, reset:%d, pTaskInfo:%p, scan type:%d", TD_VID(pVnode), __func__, req.execId, req.reset,
8,548,781✔
4002
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
4003

4004
  if (req.reset) {
8,548,781✔
4005
    int64_t uid = 0;
5,048,809✔
4006
    if (req.dynTbname && !req.pStRtFuncInfo->isMultiGroupCalc) {
5,048,809✔
4007
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
163,676✔
4008
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
163,676✔
4009
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
163,676✔
4010
        if (pValue != NULL && pValue->isTbname) {
163,676✔
4011
          uid = pValue->uid;
163,676✔
4012
          break;
163,676✔
4013
        }
4014
      }
4015
    }
4016
    
4017
    SReadHandle handle = {0};
5,048,809✔
4018
    handle.vnode = pVnode;
5,048,809✔
4019
    handle.pMsgCb = &pVnode->msgCb;
5,048,809✔
4020
    handle.pWorkerCb = pInfo->workerCb;
5,048,809✔
4021
    handle.uid = uid;
5,048,809✔
4022
    handle.cacheSttStatis = true;
5,048,809✔
4023

4024
    initStorageAPI(&handle.api);
5,048,809✔
4025
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
5,048,809✔
4026
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
3,924,786✔
4027
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
3,841,725✔
4028
      if (node != NULL) {
3,841,725✔
4029
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, false));
600,239✔
4030
      } else {
4031
        ST_TASK_DLOG("vgId:%d %s no scan time range node", TD_VID(pVnode), __func__);
3,241,486✔
4032
      }
4033

4034
      node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pExtTimeRange;
3,841,725✔
4035
      if (node != NULL) {
3,841,725✔
4036
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, true));
88,396✔
4037
      } else {
4038
        ST_TASK_DLOG("vgId:%d %s no interp time range node", TD_VID(pVnode), __func__);
3,753,329✔
4039
      }      
4040
    }
4041

4042
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
5,048,809✔
4043
    sStreamReaderCalcInfo->rtInfo.funcInfo.hasPlaceHolder = sStreamReaderCalcInfo->hasPlaceHolder;
5,048,809✔
4044
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
5,048,809✔
4045

4046
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
5,048,809✔
4047
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
811,832✔
4048
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
811,832✔
4049
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
4050
                                                    req.taskId));
4051
      STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
811,832✔
4052
    } else {
4053
      STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle));
4,236,977✔
4054
    }
4055

4056
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
5,048,809✔
4057
  }
4058

4059
  if (req.pOpParam != NULL) {
8,548,781✔
4060
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, (void*)req.pOpParam);
410,252✔
4061
  }
4062

4063
  pResList = taosArrayInit(4, POINTER_BYTES);
8,548,781✔
4064
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
8,548,781✔
4065
  uint64_t ts = 0;
8,548,781✔
4066
  STREAM_CHECK_RET_GOTO(qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL));
8,548,781✔
4067

4068
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
20,772,372✔
4069
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
12,225,272✔
4070
    if (pBlock == NULL) continue;
12,225,272✔
4071
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
12,225,272✔
4072
/*    
4073
    if (sStreamReaderCalcInfo->rtInfo.funcInfo.withExternalWindow) {
4074
      STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderCalcInfo->pFilterInfo, NULL));
4075
      printDataBlock(pBlock, __func__, "fetch filter");
4076
    }
4077
*/    
4078
  }
4079

4080
end:
8,548,781✔
4081
  code = streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision);
8,548,781✔
4082

4083
  if (sStreamReaderCalcInfo && sStreamReaderCalcInfo->rtInfo.funcInfo.isMultiGroupCalc) {
8,548,781✔
4084
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPesudoFuncVals = NULL;
×
4085
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPartColVals = NULL;
×
4086
  }
4087
  
4088
  taosArrayDestroy(pResList);
8,548,781✔
4089
  streamReleaseTask(taskAddr);
8,548,781✔
4090
  
4091
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST){
8,548,781✔
4092
    code = TDB_CODE_SUCCESS;
×
4093
  }
4094
  STREAM_PRINT_LOG_END(code, lino);
8,548,781✔
4095
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
8,548,781✔
4096
  tmsgSendRsp(&rsp);
8,548,781✔
4097
  tDestroySResFetchReq(&req);
8,548,556✔
4098
  if (TDB_CODE_SUCCESS != code) {
8,548,781✔
4099
    ST_TASK_ELOG("vgId:%d %s failed, code:%d - %s", TD_VID(pVnode), __func__,
×
4100
                 code, tstrerror(code));
4101
  }
4102
  return code;
8,548,781✔
4103
}
4104

4105
static int32_t initTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SVnode* pVnode) {
46,888,337✔
4106
  int32_t code = 0;
46,888,337✔
4107
  if (sStreamReaderInfo->tableList.pTableList != NULL) {  
46,888,337✔
4108
    return code;
46,522,065✔
4109
  }
4110
  taosWLockLatch(&sStreamReaderInfo->lock);
371,990✔
4111
  sStreamReaderInfo->pVnode = pVnode;
371,990✔
4112
  initStorageAPI(&sStreamReaderInfo->storageApi);
371,990✔
4113
  if (sStreamReaderInfo->tableList.pTableList == NULL) {
371,990✔
4114
    code = initStreamTableListInfo(&sStreamReaderInfo->tableList);
371,990✔
4115
    if (code == 0) {
371,990✔
4116
      code = generateTablistForStreamReader(pVnode, sStreamReaderInfo);
371,990✔
4117
      if (code != 0) {
371,990✔
4118
        qStreamDestroyTableInfo(&sStreamReaderInfo->tableList);
×
4119
      } else {
4120
        sStreamReaderInfo->tableList.version = pVnode->state.applied;
371,990✔
4121
        stDebug("vgId:%d %s init table list for stream reader, table num:%zu, version:%" PRId64,
371,990✔
4122
                TD_VID(pVnode), __func__, taosArrayGetSize(sStreamReaderInfo->tableList.pTableList), sStreamReaderInfo->tableList.version);
4123
      }
4124
    }
4125
  }
4126
  taosWUnLockLatch(&sStreamReaderInfo->lock);
371,990✔
4127
  return code;
371,990✔
4128
}
4129

4130
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo *pInfo) {
55,526,609✔
4131
  int32_t                   code = 0;
55,526,609✔
4132
  int32_t                   lino = 0;
55,526,609✔
4133
  SSTriggerPullRequestUnion req = {0};
55,526,609✔
4134
  void*                     taskAddr = NULL;
55,527,110✔
4135
  bool                      sendRsp = false;
55,522,021✔
4136

4137
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
55,522,021✔
4138
  if (!syncIsReadyForRead(pVnode->sync)) {
55,525,708✔
4139
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
60,431✔
4140
    return 0;
60,431✔
4141
  }
4142

4143
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
55,466,729✔
4144
    return vnodeProcessStreamFetchMsg(pVnode, pMsg, pInfo);
8,548,781✔
4145
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
46,916,202✔
4146
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
46,918,493✔
4147
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
46,919,443✔
4148
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
46,917,950✔
4149
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64 ", applied:%" PRIx64,
46,914,370✔
4150
            TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId, pVnode->state.applied);
4151
    SStreamTriggerReaderInfo* sStreamReaderInfo = qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
46,917,011✔
4152
    STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
46,916,827✔
4153
    STREAM_CHECK_RET_GOTO(initTableList(sStreamReaderInfo, pVnode));
46,891,412✔
4154
    sendRsp = true;
46,893,540✔
4155
    switch (req.base.type) {
46,893,540✔
4156
      case STRIGGER_PULL_SET_TABLE:
149,650✔
4157
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo));
149,650✔
4158
        break;
149,650✔
4159
      case STRIGGER_PULL_LAST_TS:
361,889✔
4160
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
361,889✔
4161
        break;
361,889✔
4162
      case STRIGGER_PULL_FIRST_TS:
365,448✔
4163
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
365,448✔
4164
        break;
365,448✔
4165
      case STRIGGER_PULL_TSDB_META:
531,671✔
4166
      case STRIGGER_PULL_TSDB_META_NEXT:
4167
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo));
531,671✔
4168
        break;
531,671✔
4169
      case STRIGGER_PULL_TSDB_TS_DATA:
101,647✔
4170
        if (sStreamReaderInfo->isVtableStream) {
101,647✔
4171
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo));
3,388✔
4172
        } else {
4173
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo));
98,259✔
4174
        }
4175
        break;
101,647✔
4176
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
280,081✔
4177
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
4178
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
280,081✔
4179
        break;
155,891✔
4180
      case STRIGGER_PULL_TSDB_CALC_DATA:
7,464,686✔
4181
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
4182
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
7,464,686✔
4183
        break;
7,465,415✔
4184
      case STRIGGER_PULL_TSDB_DATA:
676,238✔
4185
      case STRIGGER_PULL_TSDB_DATA_NEXT:
4186
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
676,238✔
4187
        break;
672,850✔
4188
      case STRIGGER_PULL_GROUP_COL_VALUE:
371,807✔
4189
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo));
371,807✔
4190
        break;
371,807✔
4191
      case STRIGGER_PULL_VTABLE_INFO:
87,716✔
4192
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
87,716✔
4193
        break;
87,716✔
4194
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
1,314,090✔
4195
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
1,314,090✔
4196
        break;
1,314,090✔
4197
      case STRIGGER_PULL_OTABLE_INFO:
149,650✔
4198
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
149,650✔
4199
        break;
149,650✔
4200
      case STRIGGER_PULL_WAL_META_NEW:
16,869,797✔
4201
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
16,869,797✔
4202
        break;
16,869,797✔
4203
      case STRIGGER_PULL_WAL_DATA_NEW:
11,128,937✔
4204
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
11,128,937✔
4205
        break;
11,128,664✔
4206
      case STRIGGER_PULL_WAL_META_DATA_NEW:
5,866,451✔
4207
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
5,866,451✔
4208
        break;
5,865,923✔
4209
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
1,170,270✔
4210
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
1,170,270✔
4211
        break;
1,170,270✔
4212
      default:
3,512✔
4213
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
3,512✔
4214
        sendRsp = false;
×
4215
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
4216
    }
4217
  } else {
4218
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
544✔
4219
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
4220
  }
4221
end:
46,905,572✔
4222

4223
  streamReleaseTask(taskAddr);
46,915,672✔
4224

4225
  tDestroySTriggerPullRequest(&req);
46,917,350✔
4226
  STREAM_PRINT_LOG_END(code, lino);
46,909,485✔
4227
  if (!sendRsp) {
46,913,992✔
4228
    SRpcMsg rsp = {
50,830✔
4229
      .code = code,
4230
      .pCont = pMsg->info.rsp,
25,415✔
4231
      .contLen = pMsg->info.rspLen,
25,415✔
4232
      .info = pMsg->info,
4233
    };
4234
    tmsgSendRsp(&rsp);
25,415✔
4235
  }
4236
  return code;
46,914,266✔
4237
}
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