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

taosdata / TDengine / #5024

16 Apr 2026 10:31AM UTC coverage: 72.954% (+0.7%) from 72.251%
#5024

push

travis-ci

web-flow
merge: from main to 3.0 branch #35157

120 of 200 new or added lines in 14 files covered. (60.0%)

655 existing lines in 130 files now uncovered.

273150 of 374416 relevant lines covered (72.95%)

129604715.8 hits per line

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

85.03
/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) {
7,325,818✔
65
  int64_t suid = 0;
7,325,818✔
66
  if (!sStreamReaderInfo->isVtableStream) {
7,325,818✔
67
    suid = sStreamReaderInfo->suid;
6,771,191✔
68
    goto end;
6,771,191✔
69
  }
70

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

75
  taosRLockLatch(&sStreamReaderInfo->lock);
554,627✔
76
  SStreamTableMapElement* element = taosHashGet(sStreamReaderInfo->vSetTableList.uIdMap, &pList->uid, LONG_BYTES);  
554,627✔
77
  if (element != 0) {
554,627✔
78
    suid = element->table->groupId;
308,909✔
79
    taosRUnLockLatch(&sStreamReaderInfo->lock);
308,909✔
80
    goto end;
308,909✔
81
  }
82
  taosRUnLockLatch(&sStreamReaderInfo->lock);
245,718✔
83

84
end:
7,325,818✔
85
  return suid;
7,325,818✔
86
}
87

88
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
7,407,791✔
89

90
int32_t sortCid(const void *lp, const void *rp) {
1,704,422✔
91
  int16_t* c1 = (int16_t*)lp;
1,704,422✔
92
  int16_t* c2 = (int16_t*)rp;
1,704,422✔
93

94
  if (*c1 < *c2) {
1,704,422✔
95
    return -1;
1,684,894✔
96
  } else if (*c1 > *c2) {
19,528✔
97
    return 1;
19,528✔
98
  }
99

100
  return 0;
×
101
}
102

103
int32_t sortSSchema(const void *lp, const void *rp) {
1,688,842✔
104
  SSchema* c1 = (SSchema*)lp;
1,688,842✔
105
  SSchema* c2 = (SSchema*)rp;
1,688,842✔
106

107
  if (c1->colId < c2->colId) {
1,688,842✔
108
    return -1;
1,669,314✔
109
  } else if (c1->colId > c2->colId) {
19,528✔
110
    return 1;
19,528✔
111
  }
112

113
  return 0;
×
114
}
115

116
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
41,600,717✔
117
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
41,600,717✔
118
  if (pSrc == NULL) {
41,607,354✔
119
    return terrno;
×
120
  }
121

122
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
41,607,354✔
123
  return 0;
41,601,880✔
124
}
125

126
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
9,547,896✔
127
  int32_t code = pTask->storageApi->tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
9,547,896✔
128
  if (code != TSDB_CODE_SUCCESS) {
9,547,078✔
129
    pTask->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
130
  }
131

132
  return code;
9,547,078✔
133
}
134

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

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

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

162
static bool needReLoadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
5,532,947✔
163
  if ((tableType == TD_CHILD_TABLE || tableType == TD_VIRTUAL_CHILD_TABLE) &&
5,532,947✔
164
      sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
2,458,137✔
165
      suid == sStreamReaderInfo->suid) {
896,755✔
166
    taosRLockLatch(&sStreamReaderInfo->lock);
10,566✔
167
    uint64_t gid = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
10,566✔
168
    taosRUnLockLatch(&sStreamReaderInfo->lock);
10,566✔
169
    if (gid == (uint64_t)-1) return true;
10,566✔
170
  }
171
  return false;
5,522,695✔
172
}
173

174
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id){
12,673,540✔
175
  int32_t  ret = false;
12,673,540✔
176
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
12,673,540✔
177
    if (suid != sStreamReaderInfo->suid) goto end;
7,314,298✔
178
    if (qStreamGetTableListNum(sStreamReaderInfo) == 0) goto end;
3,537,111✔
179
  } 
180
  *id = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
8,893,137✔
181
  if (*id == -1) goto end;
8,894,566✔
182
  ret = true;
5,530,577✔
183

184
end:
12,678,277✔
185
  stTrace("%s ret:%d %p %p check suid:%" PRId64 " uid:%" PRId64 " gid:%"PRIu64, __func__, ret, sStreamReaderInfo, sStreamReaderInfo->tableList.gIdMap, suid, uid, *id);
12,678,277✔
186
  return ret;
12,672,495✔
187
}
188

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

193
static bool uidInTableListSet(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc) {
59,731,606✔
194
  bool ret = false;
59,731,606✔
195
  taosRLockLatch(&sStreamReaderInfo->lock);
59,731,606✔
196
  if (sStreamReaderInfo->isVtableStream) {
59,734,069✔
197
    int64_t tmp[2] = {suid, uid};
47,091,777✔
198
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) != NULL) {
47,091,777✔
199
      *id = uid;
15,215,264✔
200
      ret = true;
15,215,264✔
201
    }
202
  } else {
203
    ret = uidInTableList(sStreamReaderInfo, suid, uid, id);
12,640,351✔
204
  }
205

206
end:
59,730,445✔
207
  taosRUnLockLatch(&sStreamReaderInfo->lock);
59,730,445✔
208
  return ret;
59,733,779✔
209
}
210

211
static int32_t  qTransformStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo, StreamTableListInfo* tableInfo){
307,801✔
212
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
307,801✔
213
  int32_t totalSize = taosArrayGetSize(pList);
307,801✔
214
  int32_t code = 0;
307,801✔
215
  void* pTask = sStreamReaderInfo->pTask;
307,801✔
216
  for (int32_t i = 0; i < totalSize; ++i) {
845,329✔
217
    STableKeyInfo* info = taosArrayGet(pList, i);
537,528✔
218
    if (info == NULL) {
537,528✔
219
      continue;
×
220
    }
221
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
537,528✔
222
    if (code != 0){
537,528✔
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);
537,528✔
227
    if (code != 0){
537,528✔
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);
537,528✔
232
    if (code != 0){
537,528✔
233
      return code;
×
234
    }
235
  }
236
  return 0;
307,801✔
237
}
238

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

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

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

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

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

299

300
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
8,588,214✔
301
  int32_t code = 0;
8,588,214✔
302
  int32_t lino = 0;
8,588,214✔
303
  void*   buf = NULL;
8,588,214✔
304
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
8,588,214✔
305
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
2,028,938✔
306
  buf = rpcMallocCont(dataEncodeSize);
2,028,938✔
307
  STREAM_CHECK_NULL_GOTO(buf, terrno);
2,028,690✔
308
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
2,028,690✔
309
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
2,028,938✔
310
  *data = buf;
2,028,938✔
311
  *size = dataEncodeSize;
2,028,938✔
312
  buf = NULL;
2,028,938✔
313
end:
8,592,441✔
314
  rpcFreeCont(buf);
8,592,441✔
315
  return code;
8,590,073✔
316
}
317

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

322
  void*   buf = NULL;
118,659✔
323

324
  int32_t blockNum = 0;
118,659✔
325
  size_t  dataEncodeBufSize = 0;
118,659✔
326
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
290,658✔
327
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
171,999✔
328
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
171,999✔
329
    int32_t blockSize = blockGetEncodeSize(pBlock);
171,999✔
330
    dataEncodeBufSize += blockSize;
171,999✔
331
    blockNum++;
171,999✔
332
  }
333
  buf = rpcMallocCont(INT_BYTES + dataEncodeBufSize);
118,659✔
334
  STREAM_CHECK_NULL_GOTO(buf, terrno);
118,423✔
335

336
  char* dataBuf = (char*)buf;
118,423✔
337
  *((int32_t*)(dataBuf)) = blockNum;
118,423✔
338
  dataBuf += INT_BYTES;
118,423✔
339
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
290,422✔
340
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
171,763✔
341
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
171,763✔
342
    int32_t actualLen = blockEncode(pBlock, dataBuf, dataEncodeBufSize, taosArrayGetSize(pBlock->pDataBlock));
171,763✔
343
    STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
171,999✔
344
    dataBuf += actualLen;
171,999✔
345
  }
346
  *data = buf;
118,659✔
347
  *size = INT_BYTES + dataEncodeBufSize;
118,659✔
348
  buf = NULL;
118,659✔
349
end:
118,659✔
350
  rpcFreeCont(buf);
118,659✔
351
  return code;
118,659✔
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) {
10,077,759✔
375
  int32_t code = 0;
10,077,759✔
376
  int32_t lino = 0;
10,077,759✔
377
  int32_t index = 0;
10,077,759✔
378
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
10,077,759✔
379
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
10,078,010✔
380
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
10,077,299✔
381
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
10,077,536✔
382

383
end:
10,077,294✔
384
  return code;
10,077,294✔
385
}
386

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

395
end:
3,288✔
396
  return code;
3,288✔
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,
30,403✔
408
                              int64_t ver) {
409
  int32_t    code = 0;
30,403✔
410
  int32_t    lino = 0;
30,403✔
411
  SDecoder   decoder = {0};
30,403✔
412
  SDeleteRes req = {0};
30,640✔
413
  void* pTask = sStreamReaderInfo->pTask;
30,640✔
414

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

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

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

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

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

451
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
2,816✔
452

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

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

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

487
static int32_t qStreamGetAddTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* uidListAdd) {
47,572✔
488
  int32_t      code = 0;
47,572✔
489
  int32_t      lino = 0;
47,572✔
490
  if (uidListAdd == NULL) {
47,572✔
491
    return 0;
41,524✔
492
  }
493
  void* pTask = sStreamReaderInfo->pTask;
6,048✔
494
  
495
  taosRLockLatch(&sStreamReaderInfo->lock);
6,048✔
496
  int32_t totalSize = taosArrayGetSize(tableListAdd);
6,048✔
497
  for (int32_t i = 0; i < totalSize; ++i) {
7,976✔
498
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
1,928✔
499
    if (info == NULL) {
1,928✔
500
      continue;
×
501
    }
502
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, &info->uid, LONG_BYTES) != NULL) {
1,928✔
503
      continue;
×
504
    }
505
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &info->uid), terrno);
3,856✔
506
    ST_TASK_WLOG("%s real add table to list for uid:%" PRId64, __func__, info->uid);
1,928✔
507
  }
508

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

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

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

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

554
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
66,402✔
555
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
33,201✔
556
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
33,201✔
557
    uint64_t id = 0;
33,201✔
558
    if(!uidInTableListOrigin(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id)) {
33,201✔
559
      continue;
32,313✔
560
    }
561

562
    if (sStreamReaderInfo->deleteOutTbl != 0) {
888✔
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) {
888✔
570
      if (uidList == NULL) {
888✔
571
        uidList = taosArrayInit(8, sizeof(tb_uid_t));
888✔
572
        STREAM_CHECK_NULL_GOTO(uidList, terrno);
888✔
573
      }
574
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pDropTbReq->uid), terrno);
1,776✔
575
    }
576
    
577
    ST_TASK_DLOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
888✔
578
  }
579
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDelOutTbl, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_DROP));
33,201✔
580

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

596
static int32_t qStreamModifyTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* tableListDel) {
49,027✔
597
  int32_t      code = 0;
49,027✔
598
  int32_t      lino = 0;
49,027✔
599
  void* pTask = sStreamReaderInfo->pTask;
49,027✔
600
  
601
  taosWLockLatch(&sStreamReaderInfo->lock);
49,027✔
602
  int32_t totalSize = taosArrayGetSize(tableListDel);
49,200✔
603
  for (int32_t i = 0; i < totalSize; ++i) {
49,200✔
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);
49,200✔
612
  for (int32_t i = 0; i < totalSize; ++i) {
78,289✔
613
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
29,262✔
614
    if (info == NULL) {
29,262✔
615
      continue;
×
616
    }
617
    int ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
29,262✔
618
    if (ret != 0){
29,262✔
619
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64",code:%d", __func__, info->uid, ret);
6,056✔
620
      continue;
6,056✔
621
    }
622
    ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag, sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
23,206✔
623
    if (ret != 0){
23,206✔
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));
23,206✔
628
    STREAM_CHECK_RET_GOTO(qStreamSetTableList(&sStreamReaderInfo->tableList, info->uid, info->groupId));
23,206✔
629
  }
630

631
end:
49,027✔
632
  taosWUnLockLatch(&sStreamReaderInfo->lock);
49,027✔
633
  return code;
49,200✔
634
}
635

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

641
  if (taosArrayGetSize(uidList) == 0) {
49,200✔
642
    return 0;
19,938✔
643
  }
644
  STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));  
29,262✔
645
  STREAM_CHECK_RET_GOTO(qStreamFilterTableListForReader(sStreamReaderInfo->pVnode, uidList, groupNew, sStreamReaderInfo->pTagCond,
29,262✔
646
                                                    sStreamReaderInfo->pTagIndexCond, &sStreamReaderInfo->storageApi,
647
                                                    sStreamReaderInfo->groupIdMap, sStreamReaderInfo->suid, tableList));
648

649
end:
29,262✔
650
  nodesDestroyList(groupNew);
29,262✔
651
  return code;
29,262✔
652
}
653

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

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

669
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
27,759✔
670
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
27,949✔
671

672
  if (sStreamReaderInfo->isVtableStream) {
27,949✔
673
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
4,800✔
674
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
4,800✔
675
  }
676
  
677
  SVCreateTbReq* pCreateReq = NULL;
27,759✔
678
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
55,708✔
679
    pCreateReq = req.pReqs + iReq;
27,759✔
680
    if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
27,759✔
681
      ST_TASK_DLOG("stream reader scan create table jump, %s", pCreateReq->name);
19,748✔
682
      continue;
19,938✔
683
    }
684
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
8,011✔
685
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
16,022✔
686
  }
687
  
688
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
27,949✔
689
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
27,949✔
690
  if (sStreamReaderInfo->isVtableStream) {
27,949✔
691
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
4,800✔
692
  }
693

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

704
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq, int64_t ver) {
5,503,791✔
705
  int32_t  code = 0;
5,503,791✔
706
  int32_t  lino = 0;
5,503,791✔
707
  void*    pTask = sStreamReaderInfo->pTask;
5,503,791✔
708
  SArray*  uidList = NULL;
5,504,704✔
709
  SArray*  tableList = NULL;
5,504,704✔
710

711
  ST_TASK_DLOG("%s start, name:%s uid:%"PRId64, __func__, pCreateReq->name, pCreateReq->uid);
5,504,751✔
712
  if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false) ||
5,506,881✔
713
      ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
2,005✔
714
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
5,503,733✔
715
    goto end;
5,503,733✔
716
  }
717
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
1,455✔
718
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
1,455✔
719
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
2,910✔
720
  ST_TASK_DLOG("stream reader scan auto create table %s", pCreateReq->name);
1,455✔
721

722
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
1,455✔
723
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
1,455✔
724
end:
5,502,367✔
725
  taosArrayDestroy(uidList);
5,505,427✔
726
  taosArrayDestroy(tableList);
5,504,477✔
727
  return code;
5,504,238✔
728
}
729

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

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

759
void getAlterColId(void* pVnode, int64_t uid, const char* colName, col_id_t* colId) {
472✔
760
  SSchemaWrapper *pSchema = metaGetTableSchema(((SVnode *)pVnode)->pMeta, uid, -1, 1, NULL, 0);
472✔
761
  if (pSchema == NULL) {
472✔
762
    return;
×
763
  }
764
  for (int32_t i = 0; i < pSchema->nCols; i++) {
1,180✔
765
    if (strncmp(pSchema->pSchema[i].name, colName, TSDB_COL_NAME_LEN) == 0) {
1,180✔
766
      *colId = pSchema->pSchema[i].colId;
472✔
767
      break;
472✔
768
    }
769
  }
770
  tDeleteSchemaWrapper(pSchema);
771
  return;
472✔
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, 
472✔
776
                                       SVAlterTbReq* pReq, uint64_t uid, int64_t ver) {
777
  int32_t code = 0;
472✔
778
  int32_t lino = 0;
472✔
779
  void* pTask = sStreamReaderInfo->pTask;
472✔
780
  SArray* uidListAdd = NULL;
472✔
781

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

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

788
  col_id_t colId = 0;
472✔
789
  getAlterColId(sStreamReaderInfo->pVnode, uid, pReq->colName, &colId);
472✔
790
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 0 && !isColIdInList(sStreamReaderInfo->triggerCols, colId)) {
472✔
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);
472✔
796
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
472✔
797

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

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

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

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

821
end:
20,268✔
822
  return code;
20,268✔
823
}
824

825
static SArray* getTableListForAlterSuperTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SVAlterTbReq* pReq){
19,796✔
826
  int32_t code = 0;
19,796✔
827
  int32_t lino = 0;
19,796✔
828
  void* pTask = sStreamReaderInfo->pTask;
19,796✔
829
  SArray* uidList = taosArrayInit(8, sizeof(tb_uid_t));
19,796✔
830
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
19,796✔
831
  for (int32_t i = 0; i < taosArrayGetSize(pReq->tables); i++) {
39,592✔
832
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
19,796✔
833
    uint64_t uid = 0;
19,796✔
834
    code = checkAlter(sStreamReaderInfo, pTable->tbName, pReq->action, &uid);
19,796✔
835
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
19,796✔
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);
19,796✔
841
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, (const void *)&uid), terrno);
19,796✔
842
  }
843

844
end:
19,796✔
845
  if (code != 0) {
19,796✔
846
    ST_TASK_ELOG("%s failed,code:%d", __func__, code);
×
847
    taosArrayDestroy(uidList);
×
848
    uidList = NULL;
×
849
  }
850
  return uidList;
19,796✔
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, 
19,796✔
855
                                    SArray* uidList, int64_t ver) {
856
  int32_t code = 0;
19,796✔
857
  int32_t lino = 0;
19,796✔
858
  void* pTask = sStreamReaderInfo->pTask;
19,796✔
859
  SArray* uidListAdd = NULL;
19,796✔
860
  SArray* uidListDel = NULL;
19,796✔
861
  SArray* tableList = NULL;
19,796✔
862

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

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

871
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
19,796✔
872
  STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
19,796✔
873

874
  if (rsp->checkAlter && taosArrayGetSize(uidListDel) > 0 && rsp->totalDataRows > 0) {
19,796✔
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));
19,796✔
882
  if (sStreamReaderInfo->isVtableStream) {
19,796✔
883
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
1,248✔
884
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
1,248✔
885
  }
886
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
19,796✔
887

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

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

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

905
  ST_TASK_DLOG("%s start", __func__);
34,926✔
906

907
  SVAlterTbReq req = {0};
34,926✔
908
  tDecoderInit(&decoder, data, len);
34,926✔
909
  
910
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
34,926✔
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 && 
34,926✔
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;
25,014✔
916
  if (req.action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || req.action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
25,014✔
917
    STREAM_CHECK_CONDITION_GOTO(!sStreamReaderInfo->isVtableStream, TDB_CODE_SUCCESS);
5,218✔
918
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
472✔
919
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
472✔
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));
472✔
925
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
19,796✔
926
    uidList = getTableListForAlterSuperTable(sStreamReaderInfo, &req);
19,796✔
927
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
19,796✔
928
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
19,796✔
929
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
×
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__);
20,268✔
943

944
end:
34,926✔
945
  destroyAlterTbReq(&req);
34,926✔
946

947
  taosArrayDestroy(uidList);
34,926✔
948
  tDecoderClear(&decoder);
34,926✔
949
  STREAM_PRINT_LOG_END_WITHID(code, lino);
34,926✔
950
  return code;
34,926✔
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) {
43,098,049✔
996
  int32_t code = 0;
43,098,049✔
997
  int32_t lino = 0;
43,098,049✔
998
  WalMetaResult walMeta = {0};
43,098,049✔
999
  SSubmitTbData submitTbData = {0};
43,099,286✔
1000
  
1001
  if (tStartDecode(pCoder) < 0) {
43,100,335✔
1002
    code = TSDB_CODE_INVALID_MSG;
×
1003
    TSDB_CHECK_CODE(code, lino, end);
×
1004
  }
1005

1006
  uint8_t       version = 0;
43,098,283✔
1007
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
43,100,921✔
1008
    code = TSDB_CODE_INVALID_MSG;
×
1009
    TSDB_CHECK_CODE(code, lino, end);
×
1010
  }
1011
  version = (submitTbData.flags >> 8) & 0xff;
43,100,921✔
1012
  submitTbData.flags = submitTbData.flags & 0xff;
43,100,921✔
1013

1014
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1015
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
43,100,921✔
1016
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
4,593,291✔
1017
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
4,592,813✔
1018
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
4,592,813✔
1019
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
4,592,786✔
1020
  }
1021

1022
  // submit data
1023
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
43,099,831✔
1024
    code = TSDB_CODE_INVALID_MSG;
×
1025
    TSDB_CHECK_CODE(code, lino, end);
×
1026
  }
1027
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
43,100,109✔
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)){
43,100,109✔
1033
    goto end;
34,888,001✔
1034
  }
1035
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
8,214,436✔
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,214,436✔
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,214,436✔
1070
    if (tDecodeU64v(pCoder, &nRow) < 0) {
8,213,722✔
1071
      code = TSDB_CODE_INVALID_MSG;
×
1072
      TSDB_CHECK_CODE(code, lino, end);
×
1073
    }
1074

1075
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
17,594,553✔
1076
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
9,376,458✔
1077
      pCoder->pos += pRow->len;
9,380,828✔
1078
      if (iRow == 0){
9,380,592✔
1079
#ifndef NO_UNALIGNED_ACCESS
1080
        walMeta.skey = pRow->ts;
8,213,546✔
1081
#else
1082
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1083
#endif
1084
      }
1085
      if (iRow == nRow - 1) {
9,380,831✔
1086
#ifndef NO_UNALIGNED_ACCESS
1087
        walMeta.ekey = pRow->ts;
8,213,785✔
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,213,483✔
1096
  if (data != NULL) {
8,213,002✔
1097
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
972✔
1098
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
972✔
1099
  } else {
1100
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
8,212,030✔
1101
  }
1102

1103
end:
43,095,239✔
1104
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
43,099,237✔
1105
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
43,099,155✔
1106
  tEndDecode(pCoder);
43,099,934✔
1107
  return code;
43,097,114✔
1108
}
1109

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

1117
  tDecoderInit(&decoder, data, len);
43,100,768✔
1118
  if (tStartDecode(&decoder) < 0) {
43,099,304✔
1119
    code = TSDB_CODE_INVALID_MSG;
×
1120
    TSDB_CHECK_CODE(code, lino, end);
×
1121
  }
1122

1123
  uint64_t nSubmitTbData = 0;
43,099,055✔
1124
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
43,100,549✔
1125
    code = TSDB_CODE_INVALID_MSG;
×
1126
    TSDB_CHECK_CODE(code, lino, end);
×
1127
  }
1128

1129
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
43,100,549✔
1130
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
43,095,999✔
1131

1132
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
86,195,365✔
1133
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash, ver));
43,097,002✔
1134
  }
1135
  tEndDecode(&decoder);
43,098,363✔
1136

1137
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
43,099,508✔
1138
  int32_t iter = 0;
43,095,490✔
1139
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
43,094,950✔
1140
  while (px != NULL) {
51,307,666✔
1141
    WalMetaResult* pMeta = (WalMetaResult*)px;
8,213,225✔
1142
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
8,213,225✔
1143
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
8,213,228✔
1144
    rsp->totalRows++;
8,212,989✔
1145
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
8,213,464✔
1146
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1147
    px = tSimpleHashIterate(gidHash, px, &iter);
8,213,464✔
1148
  }
1149
end:
43,094,441✔
1150
  tDecoderClear(&decoder);
43,096,998✔
1151
  tSimpleHashCleanup( gidHash);
43,100,071✔
1152
  return code;
43,096,679✔
1153
}
1154

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

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

1170
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
596,517✔
1171

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

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

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

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

1191
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
318,475✔
1192

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

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

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

1205
  SDecoder dcoder = {0};
510,287✔
1206
  tDecoderInit(&dcoder, data, len);
510,287✔
1207
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
510,287✔
1208
    if (rsp->deleteBlock == NULL) {
30,640✔
1209
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock));
10,898✔
1210
    }
1211
      
1212
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
30,640✔
1213
  } else if (msgType == TDMT_VND_DROP_TABLE && 
479,647✔
1214
    (sStreamReaderInfo->deleteOutTbl != 0 || sStreamReaderInfo->isVtableStream)) {
51,402✔
1215
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
33,201✔
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)) {
446,446✔
1219
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, rsp, data, len, ver));
27,949✔
1220
  } else if (msgType == TDMT_VND_ALTER_STB && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
418,497✔
1221
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
1222
  } else if (msgType == TDMT_VND_ALTER_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
393,656✔
1223
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, rsp, data, len, ver));
34,926✔
1224
  }
1225

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

1236
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
19,384,271✔
1237
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
19,379,303✔
1238
  code = walReaderSeekVer(pWalReader, rsp->ver);
19,379,303✔
1239
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
19,383,165✔
1240
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
10,677,048✔
1241
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
1242
      rsp->verTime = 0;
×
1243
    } else {
1244
      rsp->verTime = taosGetTimestampUs();
10,676,626✔
1245
    }
1246
    ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
10,677,007✔
1247
    code = TSDB_CODE_SUCCESS;
10,677,665✔
1248
    goto end;
10,677,665✔
1249
  }
1250
  STREAM_CHECK_RET_GOTO(code);
8,706,117✔
1251

1252
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
8,706,117✔
1253
  while (1) {
43,485,216✔
1254
    code = walNextValidMsg(pWalReader, true);
52,191,555✔
1255
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
52,183,251✔
1256
      rsp->verTime = taosGetTimestampUs();
8,704,435✔
1257
      ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
8,704,435✔
1258
      code = TSDB_CODE_SUCCESS;
8,704,435✔
1259
      goto end;
8,704,435✔
1260
    }
1261
    STREAM_CHECK_RET_GOTO(code);
43,479,292✔
1262
    rsp->ver = pWalReader->curVersion;
43,479,292✔
1263
    SWalCont* wCont = &pWalReader->pHead->head;
43,488,398✔
1264
    rsp->verTime = wCont->ingestTs;
43,487,617✔
1265
    if (wCont->ingestTs / 1000 > ctime) break;
43,487,403✔
1266
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
43,488,528✔
1267
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
43,487,212✔
1268
    int64_t ver = wCont->version;
43,487,017✔
1269

1270
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 "/%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d",
43,487,449✔
1271
      TD_VID(pVnode), ver, walGetAppliedVer(pWalReader->pWal), TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
1272
    if (wCont->msgType == TDMT_VND_SUBMIT) {
43,488,134✔
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) {
43,102,634✔
1275
        rsp->ver--;
2,344✔
1276
        break;
2,344✔
1277
      }
1278
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
43,100,768✔
1279
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
43,099,909✔
1280
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver));
43,100,290✔
1281
    } else {
1282
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
385,738✔
1283
    }
1284

1285
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
43,484,652✔
1286
      break;
×
1287
    }
1288
  }
1289

1290
end:
19,384,444✔
1291
  walCloseReader(pWalReader);
19,384,444✔
1292
  return code;
19,384,271✔
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) {
41,561,555✔
1296
  int32_t     code = 0;
41,561,555✔
1297
  int32_t     lino = 0;
41,561,555✔
1298
  SMetaReader mr = {0};
41,561,555✔
1299
  SArray* tagCache = NULL;
41,562,649✔
1300
  char* data = NULL;
41,565,082✔
1301

1302
  if (lock != NULL) taosWLockLatch(lock);
41,567,503✔
1303
  STREAM_CHECK_CONDITION_GOTO(numOfExpr == 0, code);
41,572,483✔
1304
  stDebug("%s start,uid:%"PRIu64, __func__, uid);
2,287,134✔
1305
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
2,287,521✔
1306
  if (uidData == NULL) {
2,287,134✔
1307
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
2,267,992✔
1308
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
2,267,992✔
1309
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
2,267,992✔
1310
      taosArrayDestroy(tagCache);
×
1311
      code = terrno;
×
1312
      goto end;
×
1313
    }
1314
  } else {
1315
    tagCache = *(SArray**)uidData;
19,142✔
1316
    stDebug("%s found tagCache, size:%zu %d, uid:%"PRIu64, __func__, taosArrayGetSize(tagCache), numOfExpr, uid);
19,142✔
1317
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
19,142✔
1318
  }
1319
  
1320
  api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn);
2,287,134✔
1321
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
2,287,134✔
1322
  api->metaReaderFn.readerReleaseLock(&mr);
2,286,747✔
1323
  STREAM_CHECK_RET_GOTO(code);
2,286,167✔
1324
  
1325
  for (int32_t j = 0; j < numOfExpr; ++j) {
7,702,584✔
1326
    const SExprInfo* pExpr1 = &pExprInfo[j];
5,421,893✔
1327
    int32_t functionId = pExpr1->pExpr->_function.functionId;
5,419,958✔
1328
    col_id_t cid = 0;
5,424,408✔
1329
    // this is to handle the tbname
1330
    if (fmIsScanPseudoColumnFunc(functionId)) {
5,424,408✔
1331
      int32_t fType = pExpr1->pExpr->_function.functionType;
663,247✔
1332
      if (fType == FUNCTION_TYPE_TBNAME) {
663,247✔
1333
        data = taosMemoryCalloc(1, strlen(mr.me.name) + VARSTR_HEADER_SIZE);
663,247✔
1334
        STREAM_CHECK_NULL_GOTO(data, terrno);
663,247✔
1335
        STR_TO_VARSTR(data, mr.me.name)
663,247✔
1336
      }
1337
      cid = -1;
663,247✔
1338
    } else {  // these are tags
1339
      const char* p = NULL;
4,758,357✔
1340
      char* pData = NULL;
4,758,357✔
1341
      int8_t type = pExpr1->base.resSchema.type;
4,758,357✔
1342
      int32_t len = pExpr1->base.resSchema.bytes;
4,759,310✔
1343
      STagVal tagVal = {0};
4,759,310✔
1344
      tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
4,759,310✔
1345
      cid = tagVal.cid;
4,759,697✔
1346
      if (colId != 0 && cid != colId) {
4,759,697✔
1347
        continue;
1,361✔
1348
      }
1349
      p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, type, &tagVal);
4,758,336✔
1350

1351
      if (type != TSDB_DATA_TYPE_JSON && p != NULL) {
4,757,895✔
1352
        pData = tTagValToData((const STagVal*)p, false);
4,754,435✔
1353
      } else {
1354
        pData = (char*)p;
3,460✔
1355
      }
1356

1357
      if (pData != NULL && (type == TSDB_DATA_TYPE_JSON || !IS_VAR_DATA_TYPE(type))) {
4,757,955✔
1358
        if (type == TSDB_DATA_TYPE_JSON) {
2,344,118✔
1359
          len = getJsonValueLen(pData);
×
1360
        }
1361
        data = taosMemoryCalloc(1, len);
2,344,118✔
1362
        STREAM_CHECK_NULL_GOTO(data, terrno);
2,343,641✔
1363
        (void)memcpy(data, pData, len);
2,343,641✔
1364
      } else {
1365
        data = pData;
2,413,837✔
1366
      }
1367
    }
1368
    if (uidData == NULL){
5,421,886✔
1369
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
10,770,371✔
1370
    } else {
1371
      void* pre = taosArrayGetP(tagCache, j);
36,507✔
1372
      taosMemoryFree(pre);
36,507✔
1373
      taosArraySet(tagCache, j, &data);
36,507✔
1374
    }
1375
    data = NULL;
5,421,112✔
1376
  }
1377

1378
end:
41,571,528✔
1379
  taosMemoryFree(data);
41,572,387✔
1380
  api->metaReaderFn.clearReader(&mr);
41,563,558✔
1381
  if (lock != NULL) taosWUnLockLatch(lock);
41,565,411✔
1382
  return code;
41,566,530✔
1383
}
1384

1385
int32_t fillTag(SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr,
248,222,422✔
1386
                uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks, SRWLatch* lock) {
1387
  int32_t     code = 0;
248,222,422✔
1388
  int32_t     lino = 0;
248,222,422✔
1389
  SArray* tagCache = NULL;
248,222,422✔
1390
  if (numOfExpr == 0) {
248,222,422✔
1391
    return TSDB_CODE_SUCCESS;
38,724,189✔
1392
  }
1393

1394
  taosRLockLatch(lock);
209,498,233✔
1395
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
209,882,296✔
1396
  if (uidData == NULL) {
209,651,684✔
1397
    stError("%s error uidData is null,uid:%"PRIu64, __func__, uid);
×
1398
  } else {
1399
    tagCache = *(SArray**)uidData;
209,651,684✔
1400
    if(taosArrayGetSize(tagCache) != numOfExpr) {
209,716,966✔
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,139,943,518✔
1407
    const SExprInfo* pExpr1 = &pExprInfo[j];
929,667,222✔
1408
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
929,838,977✔
1409

1410
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
929,977,327✔
1411
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
928,205,859✔
1412
    int32_t functionId = pExpr1->pExpr->_function.functionId;
928,205,859✔
1413

1414
    // this is to handle the tbname
1415
    if (fmIsScanPseudoColumnFunc(functionId)) {
928,561,784✔
1416
      int32_t fType = pExpr1->pExpr->_function.functionType;
3,742,046✔
1417
      if (fType == FUNCTION_TYPE_TBNAME) {
3,742,046✔
1418
        pColInfoData->info.colId = -1;
3,742,046✔
1419
      }
1420
    } 
1421
    char* data = tagCache == NULL ? NULL : taosArrayGetP(tagCache, j);
928,299,263✔
1422

1423
    bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
926,739,023✔
1424
    if (isNullVal) {
927,059,332✔
1425
      colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
1426
    } else {
1427
      if (!IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
927,059,332✔
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);
927,606,354✔
1433
      STREAM_CHECK_RET_GOTO(code);
930,062,906✔
1434
    }
1435
  }
1436
end:
210,276,296✔
1437
  taosRUnLockLatch(lock);
210,276,609✔
1438
  return code;
209,923,035✔
1439
}
1440

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

1446
  void* pTask = info->pTask;
3,631,101✔
1447
  ST_TASK_DLOG("%s start. rows:%" PRIu32 ",uid:%"PRIu64, __func__,  numOfRows, uid);
3,631,101✔
1448
  
1449
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
3,631,355✔
1450
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
3,631,355✔
1451
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
3,631,119✔
1452
  
1453
  code = fillTag(metaCache, pExprInfo, numOfExpr, uid, pBlock, currentRow, numOfRows, numOfBlocks, &info->lock);
3,631,119✔
1454
  STREAM_CHECK_RET_GOTO(code);
3,630,862✔
1455

1456
end:
3,630,862✔
1457
  return code;
3,630,862✔
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) {
22,628,555✔
1506
  int32_t code = 0;
22,628,555✔
1507
  int32_t lino = 0;
22,628,555✔
1508
  int64_t id[2] = {suid, uid};
22,628,555✔
1509
  taosRLockLatch(&sStreamReaderInfo->lock);
22,630,051✔
1510
  void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
22,629,815✔
1511
  STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
22,628,376✔
1512
  SSHashObj* uInfo = *(SSHashObj **)px;
22,628,376✔
1513
  STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
22,628,376✔
1514
  int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
22,628,376✔
1515
  if (tmp != NULL) {
22,629,098✔
1516
    *colId = *tmp;
20,793,358✔
1517
  } else {
1518
    *colId = -1;
1,835,740✔
1519
  }
1520

1521
end:
22,629,812✔
1522
  taosRUnLockLatch(&sStreamReaderInfo->lock);
22,629,098✔
1523
  return code;
22,629,573✔
1524
}
1525

1526
static int32_t getSchemas(SVnode* pVnode, int64_t suid, int64_t uid, int32_t sver, SStreamTriggerReaderInfo* sStreamReaderInfo, STSchema** schema) {
10,661,840✔
1527
  int32_t code = 0;
10,661,840✔
1528
  int32_t lino = 0;
10,661,840✔
1529
  int64_t id = suid != 0 ? suid : uid;
10,661,840✔
1530
  if (sStreamReaderInfo->isVtableStream) {
10,662,619✔
1531
    STSchema** schemaTmp = taosHashGet(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES);
7,267,218✔
1532
    if (schemaTmp == NULL || *schemaTmp == NULL || (*schemaTmp)->version != sver) {
7,266,979✔
1533
      *schema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
65,045✔
1534
      STREAM_CHECK_NULL_GOTO(*schema, terrno);
65,045✔
1535
      code = taosHashPut(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES, schema, POINTER_BYTES);
64,826✔
1536
      if (code != 0) {
65,045✔
1537
        taosMemoryFree(*schema);
×
1538
        goto end;
×
1539
      }
1540
    } else {
1541
      *schema = *schemaTmp;
7,201,934✔
1542
    }
1543
  } else {
1544
    if (sStreamReaderInfo->triggerTableSchema == NULL || sStreamReaderInfo->triggerTableSchema->version != sver) {
3,395,401✔
1545
      taosMemoryFree(sStreamReaderInfo->triggerTableSchema);
86,769✔
1546
      sStreamReaderInfo->triggerTableSchema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
86,769✔
1547
      STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerTableSchema, terrno);
86,769✔
1548
    }
1549
    *schema = sStreamReaderInfo->triggerTableSchema;
3,395,873✔
1550
  }
1551
  
1552
end:
10,662,852✔
1553
  return code;
10,662,852✔
1554
}
1555

1556
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, 
11,676,243✔
1557
  SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
1558
  int32_t code = 0;
11,676,243✔
1559
  int32_t lino = 0;
11,676,243✔
1560
  uint64_t id = 0;
11,676,243✔
1561
  WalMetaResult walMeta = {0};
11,676,479✔
1562
  void* pTask = sStreamReaderInfo->pTask;
11,676,243✔
1563
  SSDataBlock * pBlock = (SSDataBlock*)rsp->dataBlock;
11,675,994✔
1564

1565
  if (tStartDecode(pCoder) < 0) {
11,675,522✔
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};
11,675,536✔
1572
  uint8_t       version = 0;
11,675,064✔
1573
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
11,675,797✔
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;
11,675,797✔
1579
  submitTbData.flags = submitTbData.flags & 0xff;
11,675,797✔
1580
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1581
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,675,797✔
1582
    if (tStartDecode(pCoder) < 0) {
107,002✔
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);
107,002✔
1588
  }
1589

1590
  // submit data
1591
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
11,675,772✔
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) {
11,675,995✔
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);
11,675,995✔
1603

1604
  if (rsp->uidHash != NULL) {
11,676,243✔
1605
    uint64_t* gid = tSimpleHashGet(rsp->uidHash, &submitTbData.uid, LONG_BYTES);
8,818,117✔
1606
    STREAM_CHECK_CONDITION_GOTO(gid == NULL, TDB_CODE_SUCCESS);
8,817,881✔
1607
    ST_TASK_DLOG("%s get uid gid from uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%"PRIu64, __func__, submitTbData.uid, submitTbData.suid, *gid);
8,817,881✔
1608
    id = *gid;
8,817,881✔
1609
  } else {
1610
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &id, rsp->isCalc), TDB_CODE_SUCCESS);
2,858,362✔
1611
  }
1612

1613
  walMeta.id = id;
10,661,439✔
1614
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
10,661,439✔
1615

1616
  if (ranges != NULL){
10,661,899✔
1617
    void* timerange = tSimpleHashGet(ranges, &id, sizeof(id));
8,817,633✔
1618
    if (timerange == NULL) goto end;;
8,817,881✔
1619
    int64_t* pRange = (int64_t*)timerange;
8,817,881✔
1620
    window.skey = pRange[0];
8,817,881✔
1621
    window.ekey = pRange[1];
8,817,881✔
1622
    ST_TASK_DLOG("%s get time range from ranges, uid:%" PRId64 ", suid:%" PRId64 ", gid:%" PRIu64 ", skey:%" PRId64 ", ekey:%" PRId64,
8,817,881✔
1623
      __func__, submitTbData.uid, submitTbData.suid, id, window.skey, window.ekey);
1624
  }
1625
  
1626
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,662,147✔
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;
10,662,147✔
1633
  STREAM_CHECK_RET_GOTO(getSchemas(pVnode, submitTbData.suid, submitTbData.uid, submitTbData.sver, sStreamReaderInfo, &schema));
10,661,895✔
1634

1635
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, &submitTbData.uid, LONG_BYTES);
10,662,379✔
1636
  int32_t blockStart = 0;
10,662,843✔
1637
  int32_t numOfRows = 0;
10,662,843✔
1638
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,662,843✔
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;
10,662,843✔
1722
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,662,371✔
1723
      code = TSDB_CODE_INVALID_MSG;
×
1724
      TSDB_CHECK_CODE(code, lino, end);
×
1725
    }
1726
    for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
22,613,409✔
1727
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
11,950,526✔
1728
      pCoder->pos += pRow->len;
11,950,998✔
1729

1730
      if (iRow == 0){
11,950,998✔
1731
#ifndef NO_UNALIGNED_ACCESS
1732
        walMeta.skey = pRow->ts;
10,662,843✔
1733
#else
1734
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1735
#endif
1736
      }
1737
      if (iRow == nRow - 1) {
11,950,762✔
1738
#ifndef NO_UNALIGNED_ACCESS
1739
        walMeta.ekey = pRow->ts;
10,662,607✔
1740
#else
1741
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
1742
#endif
1743
      }
1744

1745
      if (pRow->ts < window.skey || pRow->ts > window.ekey) {
11,950,762✔
1746
        continue;
7,270✔
1747
      }
1748
      STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
11,942,996✔
1749
      blockStart = pSlice->currentRowIdx;
11,942,788✔
1750
     
1751
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {  // reader todo test null
73,863,895✔
1752
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
61,918,568✔
1753
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
61,918,934✔
1754
        if (pColData->info.colId <= -1) {
61,918,934✔
1755
          pColData->hasNull = true;
21,019,267✔
1756
          continue;
21,019,267✔
1757
        }
1758
        int16_t colId = 0;
40,906,074✔
1759
        if (sStreamReaderInfo->isVtableStream){
40,906,074✔
1760
          STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
22,628,077✔
1761
          ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
22,629,573✔
1762
        } else {
1763
          colId = pColData->info.colId;
18,277,524✔
1764
        }
1765
        
1766
        SColVal colVal = {0};
40,907,097✔
1767
        int32_t sourceIdx = 0;
40,907,097✔
1768
        while (1) {
1769
          if (sourceIdx >= schema->numOfCols) {
111,747,305✔
1770
            break;
9,069,635✔
1771
          }
1772
          STREAM_CHECK_RET_GOTO(tRowGet(pRow, schema, sourceIdx, &colVal));
102,673,091✔
1773
          if (colVal.cid == colId) {
102,680,021✔
1774
            break;
31,839,813✔
1775
          }
1776
          sourceIdx++;
70,840,208✔
1777
        }
1778
        if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
40,909,448✔
1779
          if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL){
29,407,394✔
1780
            STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart+ numOfRows, (const char*)colVal.value.pData, colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
28,482✔
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, 
26,757✔
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)));
29,378,912✔
1785
          }
1786
        } else {
1787
          colDataSetNULL(pColData, blockStart + numOfRows);
11,502,054✔
1788
        }
1789
      }
1790
      
1791
      numOfRows++;
11,943,768✔
1792
    }
1793
  }
1794

1795
  if (numOfRows > 0) {
10,662,883✔
1796
    if (!sStreamReaderInfo->isVtableStream) {
10,662,883✔
1797
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, rsp->isCalc, submitTbData.uid, pBlock, blockStart, numOfRows, 1));
3,395,665✔
1798
    }
1799
    
1800
    SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
10,662,390✔
1801
    STREAM_CHECK_NULL_GOTO(pColData, terrno);
10,662,390✔
1802
    STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
10,662,390✔
1803

1804
    STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
10,662,139✔
1805
    ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
10,662,139✔
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;
10,662,148✔
1809
    pBlock->info.rows += numOfRows;
10,662,160✔
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;
10,662,387✔
1816

1817
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
1,844,518✔
1818
  if (data != NULL) {
1,844,530✔
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)));
1,844,530✔
1823
  }
1824

1825
end:
11,672,846✔
1826
  if (code != 0) {                                                             \
11,675,742✔
1827
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code)); \
208✔
1828
  }
1829
  tEndDecode(pCoder);
11,675,742✔
1830
  return code;
11,676,703✔
1831
}
1832
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
11,674,587✔
1833
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
1834
  int32_t  code = 0;
11,674,587✔
1835
  int32_t  lino = 0;
11,674,587✔
1836
  SDecoder decoder = {0};
11,674,587✔
1837
  SSHashObj* gidHash = NULL;
11,674,823✔
1838
  void* pTask = sStreamReaderInfo->pTask;
11,674,823✔
1839

1840
  tDecoderInit(&decoder, data, len);
11,675,059✔
1841
  if (tStartDecode(&decoder) < 0) {
11,673,875✔
1842
    code = TSDB_CODE_INVALID_MSG;
×
1843
    TSDB_CHECK_CODE(code, lino, end);
×
1844
  }
1845

1846
  uint64_t nSubmitTbData = 0;
11,675,769✔
1847
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
11,675,534✔
1848
    code = TSDB_CODE_INVALID_MSG;
×
1849
    TSDB_CHECK_CODE(code, lino, end);
×
1850
  }
1851

1852
  if (rsp->metaBlock != NULL){
11,675,534✔
1853
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
2,857,418✔
1854
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
2,856,946✔
1855
  }
1856

1857
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
23,351,320✔
1858
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, ranges, gidHash, rsp, ver));
11,674,578✔
1859
  }
1860

1861
  tEndDecode(&decoder);
11,676,742✔
1862

1863
  if (rsp->metaBlock != NULL){
11,675,091✔
1864
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
2,856,974✔
1865
    int32_t iter = 0;
2,856,738✔
1866
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
2,856,738✔
1867
    while (px != NULL) {
4,702,435✔
1868
      WalMetaResult* pMeta = (WalMetaResult*)px;
1,843,809✔
1869
      STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
1,843,809✔
1870
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
1,843,810✔
1871
      rsp->totalRows++;
1,843,810✔
1872
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
1,843,810✔
1873
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1874
      px = tSimpleHashIterate(gidHash, px, &iter);
1,844,306✔
1875
    }
1876
  }
1877
  
1878

1879
end:
11,672,899✔
1880
  tSimpleHashCleanup(gidHash);
11,676,703✔
1881
  tDecoderClear(&decoder);
11,676,715✔
1882
  return code;
11,676,248✔
1883
}
1884

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

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

1896
  SSubmitTbData submitTbData = {0};
13,751,918✔
1897
  uint8_t       version = 0;
13,751,446✔
1898
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
13,752,867✔
1899
    code = TSDB_CODE_INVALID_MSG;
×
1900
    TSDB_CHECK_CODE(code, lino, end);
×
1901
  }
1902
  version = (submitTbData.flags >> 8) & 0xff;
13,752,867✔
1903
  submitTbData.flags = submitTbData.flags & 0xff;
13,752,867✔
1904

1905
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1906
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
13,752,867✔
1907
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
911,220✔
1908
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
910,984✔
1909
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
910,984✔
1910
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
910,683✔
1911
  }
1912

1913
  // submit data
1914
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
13,751,215✔
1915
    code = TSDB_CODE_INVALID_MSG;
×
1916
    TSDB_CHECK_CODE(code, lino, end);
×
1917
  }
1918
  if (tDecodeI64(pCoder, uid) < 0) {
13,751,450✔
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);
13,751,450✔
1923
  STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, *uid, gid, rsp->isCalc), TDB_CODE_SUCCESS);
13,750,209✔
1924
  if (rsp->uidHash != NULL) {
10,663,944✔
1925
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->uidHash, uid, LONG_BYTES, gid, LONG_BYTES));
8,817,878✔
1926
    ST_TASK_DLOG("%s put uid into uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%"PRIu64, __func__, *uid, submitTbData.suid, *gid);
8,818,117✔
1927
  }
1928
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
10,664,419✔
1929

1930
  if (ranges != NULL){
10,664,419✔
1931
    void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
8,818,117✔
1932
    if (timerange == NULL) goto end;;
8,818,117✔
1933
    int64_t* pRange = (int64_t*)timerange;
8,818,117✔
1934
    window.skey = pRange[0];
8,818,117✔
1935
    window.ekey = pRange[1];
8,818,117✔
1936
  }
1937
  
1938
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,666,026✔
1939
    code = TSDB_CODE_INVALID_MSG;
×
1940
    TSDB_CHECK_CODE(code, lino, end);
×
1941
  }
1942

1943
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,666,026✔
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;
10,666,026✔
1970
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,666,329✔
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) { 
10,666,329✔
1976
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
18,710,044✔
1977
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
9,892,130✔
1978
        pCoder->pos += pRow->len;
9,892,130✔
1979
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
9,892,130✔
1980
          continue;
7,270✔
1981
        }
1982
        (*numOfRows)++;
9,885,332✔
1983
      }
1984
    } else {
1985
      (*numOfRows) = nRow;
1,848,650✔
1986
    }
1987
  }
1988
  
1989
end:
13,752,382✔
1990
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
13,751,436✔
1991
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
13,749,313✔
1992
  tEndDecode(pCoder);
13,749,065✔
1993
  return code;
13,749,559✔
1994
}
1995

1996
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
13,750,506✔
1997
  int32_t  code = 0;
13,750,506✔
1998
  int32_t  lino = 0;
13,750,506✔
1999
  SDecoder decoder = {0};
13,750,506✔
2000
  void* pTask = sStreamReaderInfo->pTask;
13,751,450✔
2001

2002
  tDecoderInit(&decoder, data, len);
13,750,506✔
2003
  if (tStartDecode(&decoder) < 0) {
13,746,714✔
2004
    code = TSDB_CODE_INVALID_MSG;
×
2005
    TSDB_CHECK_CODE(code, lino, end);
×
2006
  }
2007

2008
  uint64_t nSubmitTbData = 0;
13,750,476✔
2009
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
13,751,194✔
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);
13,751,194✔
2014

2015
  for (int32_t i = 0; i < nSubmitTbData; i++) {
27,503,374✔
2016
    uint64_t gid = -1;
13,750,979✔
2017
    int64_t  uid = 0;
13,749,798✔
2018
    int32_t numOfRows = 0;
13,750,742✔
2019
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp, ver));
13,750,271✔
2020
    if (numOfRows <= 0) {
13,750,270✔
2021
      ST_TASK_DLOG("%s no valid data uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, ver:%"PRId64, __func__, uid, gid, numOfRows, ver);
3,085,144✔
2022
      continue;
3,085,380✔
2023
    }
2024
    rsp->totalRows += numOfRows;
10,665,126✔
2025
    rsp->totalDataRows += numOfRows;
10,665,362✔
2026

2027
    SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, &uid, LONG_BYTES);
10,666,306✔
2028
    if (pSlice != NULL) {
10,667,251✔
2029
      pSlice->numRows += numOfRows;
9,949,548✔
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);
9,949,548✔
2031
      pSlice->gId = gid;
9,949,300✔
2032
    } else {
2033
      SStreamWalDataSlice tmp = {.gId=gid,.numRows=numOfRows,.currentRowIdx=0,.startRowIdx=0};
717,703✔
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);
717,703✔
2035
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->indexHash, &uid, LONG_BYTES, &tmp, sizeof(tmp)));
717,703✔
2036
    } 
2037
  }
2038

2039
  tEndDecode(&decoder);
13,752,395✔
2040

2041
end:
13,753,103✔
2042
  tDecoderClear(&decoder);
13,752,395✔
2043
  return code;
13,751,910✔
2044
}
2045

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

2060
static void printIndexHash(SSHashObj* indexHash, void* pTask){
601,093✔
2061
  if (qDebugFlag & DEBUG_TRACE) {
601,093✔
2062
    void*   pe = NULL;
8,664✔
2063
    int32_t iter = 0;
8,664✔
2064
    while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
20,839✔
2065
      SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
12,175✔
2066
      ST_TASK_TLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
12,399✔
2067
      pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
2068
    }
2069
  }
2070
}
601,093✔
2071

2072
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
12,513✔
2073
  void*   pe = NULL;
12,513✔
2074
  int32_t iter = 0;
12,513✔
2075
  int32_t index = 0;
12,513✔
2076
  int32_t pIndex = 0;
12,513✔
2077
  int8_t* pIndicator = (int8_t*)pRet->pData;
12,513✔
2078
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
26,652✔
2079
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
14,139✔
2080
    pInfo->startRowIdx = index;
14,139✔
2081
    int32_t size = pInfo->numRows;
14,139✔
2082
    for (int32_t i = 0; i < pInfo->numRows; i++) {
204,279✔
2083
      if (pIndicator && !pIndicator[pIndex++]) {
190,140✔
2084
        size--;
63,658✔
2085
      }
2086
    }
2087
    pInfo->numRows = size;
14,139✔
2088
    index += pInfo->numRows;
14,139✔
2089
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
14,139✔
2090
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
2091
  }
2092
}
12,513✔
2093

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

2099
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
3,764,358✔
2100
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
3,761,021✔
2101
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
3,262,744✔
2102
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
2103
      resultRsp->verTime = 0;
×
2104
    } else {
2105
      resultRsp->verTime = taosGetTimestampUs();
3,261,604✔
2106
    }
2107
    ST_TASK_DLOG("%s scan wal end:%s",  __func__, tstrerror(code));
3,263,307✔
2108
    code = TSDB_CODE_SUCCESS;
3,263,703✔
2109
    goto end;
3,263,703✔
2110
  }
2111
  STREAM_CHECK_RET_GOTO(code);
498,277✔
2112

2113
  while (1) {
5,057,882✔
2114
    code = walNextValidMsg(pWalReader, true);
5,556,159✔
2115
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
5,555,063✔
2116
      resultRsp->verTime = taosGetTimestampUs();
499,017✔
2117
      ST_TASK_DLOG("%s scan wal end:%s", __func__, tstrerror(code));
499,017✔
2118
      code = TSDB_CODE_SUCCESS;
499,017✔
2119
      goto end;
499,017✔
2120
    }
2121
    STREAM_CHECK_RET_GOTO(code);
5,055,574✔
2122
    resultRsp->ver = pWalReader->curVersion;
5,055,574✔
2123
    SWalCont* wCont = &pWalReader->pHead->head;
5,056,157✔
2124
    resultRsp->verTime = wCont->ingestTs;
5,056,641✔
2125
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
5,056,417✔
2126
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
5,057,159✔
2127
    int64_t ver = wCont->version;
5,057,033✔
2128
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d, msg len:%d", __func__,
5,056,906✔
2129
      ver, TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl, len);
2130
    if (wCont->msgType == TDMT_VND_SUBMIT) {
5,057,989✔
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) {
4,932,377✔
2133
        resultRsp->ver--;
×
2134
        break;
×
2135
      }
2136
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
4,932,377✔
2137
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
4,933,569✔
2138
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp, ver));
4,933,569✔
2139
    } else {
2140
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
124,549✔
2141
    }
2142

2143
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
5,056,690✔
2144
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM || resultRsp->needReturn) {
5,056,478✔
2145
      break;
2146
    }
2147
  }
2148
  
2149
end:
1✔
2150
  STREAM_PRINT_LOG_END(code, lino);
3,762,721✔
2151
  return code;
3,763,194✔
2152
}
2153

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

2160
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
23,001,493✔
2161
    int64_t *ver = taosArrayGet(versions, i);
8,816,860✔
2162
    if (ver == NULL) continue;
8,818,117✔
2163

2164
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
8,818,117✔
2165
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
8,817,338✔
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));
8,817,338✔
2171

2172
    SWalCont* wCont = &pWalReader->pHead->head;
8,818,117✔
2173
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
8,817,881✔
2174
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
8,818,117✔
2175

2176
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp, *ver));
8,817,881✔
2177
  }
2178
  
2179
end:
14,183,823✔
2180
  return code;
14,183,823✔
2181
}
2182

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

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

2191
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
601,093✔
2192
    filterIndexHash(resultRsp->indexHash, pRet);
12,513✔
2193
  }
2194

2195
end:
601,093✔
2196
  colDataDestroy(pRet);
601,093✔
2197
  taosMemoryFree(pRet);
601,093✔
2198
  return code;
601,093✔
2199
}
2200

2201
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
3,763,051✔
2202
                                    SSTriggerWalNewRsp* resultRsp) {
2203
  int32_t      code = 0;
3,763,051✔
2204
  int32_t      lino = 0;
3,763,051✔
2205
  void* pTask = sStreamReaderInfo->pTask;
3,763,051✔
2206
                                        
2207
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
3,763,258✔
2208
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
3,761,964✔
2209
  blockDataEmpty(resultRsp->dataBlock);
3,761,964✔
2210
  blockDataEmpty(resultRsp->metaBlock);
3,759,191✔
2211
  int64_t lastVer = resultRsp->ver;                                      
3,760,956✔
2212
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp));
3,761,192✔
2213
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
3,762,985✔
2214

2215
  buildIndexHash(resultRsp->indexHash, pTask);
134,766✔
2216
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
134,766✔
2217
  while(lastVer < resultRsp->ver) {
3,036,610✔
2218
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
2,902,299✔
2219
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
2,900,904✔
2220
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
43,974✔
2221
      continue;
43,974✔
2222
    }
2223
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
2,857,166✔
2224
    SWalCont* wCont = &pWalReader->pHead->head;
2,856,485✔
2225
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
2,856,957✔
2226
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
2,856,721✔
2227
    ST_TASK_DLOG("process wal ver:%" PRId64 ", type:%d, bodyLen:%d", wCont->version, wCont->msgType, bodyLen);
2,856,957✔
2228
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
2,856,957✔
2229
  }
2230

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

2235
end:
3,763,430✔
2236
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
3,763,430✔
2237
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
2238
  walCloseReader(pWalReader);
3,762,859✔
2239
  return code;
3,762,484✔
2240
}
2241

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

2247
  void* pTask = sStreamReaderInfo->pTask;
14,183,283✔
2248
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
14,184,841✔
2249
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
14,183,075✔
2250
  
2251
  if (taosArrayGetSize(versions) > 0) {
14,183,075✔
2252
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
466,535✔
2253
  }
2254
  
2255
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
14,184,062✔
2256
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
14,183,615✔
2257

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

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

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

2269
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
8,817,898✔
2270
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
8,818,117✔
2271
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
2272
      continue;
×
2273
    }
2274
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
8,818,117✔
2275
    SWalCont* wCont = &pWalReader->pHead->head;
8,817,642✔
2276
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
8,817,642✔
2277
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
8,817,642✔
2278

2279
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
8,817,642✔
2280
  }
2281
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
2282
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
466,535✔
2283
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
466,535✔
2284

2285
end:
14,184,363✔
2286
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
14,184,363✔
2287
            rsp->totalRows, rsp->ver, walGetAppliedVer(pWalReader->pWal));
2288
  walCloseReader(pWalReader);
14,186,908✔
2289
  return code;
14,185,142✔
2290
}
2291

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

2302
  SSchemaWrapper* sSchemaWrapper = NULL;
438,858✔
2303
  if (metaReader.me.type == TD_CHILD_TABLE) {
438,858✔
2304
    int64_t suid = metaReader.me.ctbEntry.suid;
438,858✔
2305
    tDecoderClear(&metaReader.coder);
438,858✔
2306
    STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, suid));
438,858✔
2307
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
438,858✔
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++) {
2,590,228✔
2315
    SSchema* s = sSchemaWrapper->pSchema + j;
2,151,370✔
2316
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
4,302,740✔
2317
  }
2318

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

2329
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
438,858✔
2330
  int32_t code = 0;
438,858✔
2331
  int32_t lino = 0;
438,858✔
2332
  size_t  schemaLen = taosArrayGetSize(schemas);
438,858✔
2333
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
438,858✔
2334
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
1,714,294✔
2335
    col_id_t* id = taosArrayGet(cols, i);
1,275,436✔
2336
    STREAM_CHECK_NULL_GOTO(id, terrno);
1,275,436✔
2337
    for (size_t i = 0; i < schemaLen; i++) {
3,332,099✔
2338
      SSchema* s = taosArrayGet(schemas, i);
3,332,099✔
2339
      STREAM_CHECK_NULL_GOTO(s, terrno);
3,332,099✔
2340
      if (*id == s->colId) {
3,332,099✔
2341
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
1,275,436✔
2342
        break;
1,275,436✔
2343
      }
2344
    }
2345
  }
2346
  taosArrayPopFrontBatch(schemas, schemaLen);
438,858✔
2347

2348
end:
438,858✔
2349
  return code;
438,858✔
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,
610,422✔
2476
                                    STimeRangeNode* node, SReadHandle* handle, bool isExtWin) {
2477
  int32_t code = 0;
610,422✔
2478
  int32_t lino = 0;
610,422✔
2479
  void* pTask = sStreamReaderCalcInfo->pTask;
610,422✔
2480
  STimeWindow* pWin = isExtWin ? &handle->extWinRange : &handle->winRange;
610,422✔
2481
  bool* pValid = isExtWin ? &handle->extWinRangeValid : &handle->winRangeValid;
610,422✔
2482
  
2483
  if (req->pStRtFuncInfo->withExternalWindow) {
610,422✔
2484
    sStreamReaderCalcInfo->tmpRtFuncInfo.curIdx = 0;
466,563✔
2485
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
466,563✔
2486
    sStreamReaderCalcInfo->tmpRtFuncInfo.isWindowTrigger = req->pStRtFuncInfo->isWindowTrigger;
466,438✔
2487
    sStreamReaderCalcInfo->tmpRtFuncInfo.precision = req->pStRtFuncInfo->precision;
466,438✔
2488

2489
    SSTriggerCalcParam* pFirst = NULL;
466,438✔
2490
    SSTriggerCalcParam* pLast = NULL;
466,438✔
2491
    if (req->pStRtFuncInfo->isMultiGroupCalc) {
466,438✔
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);
466,438✔
2497
      pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
466,563✔
2498
      STREAM_CHECK_NULL_GOTO(pFirst, terrno);
466,563✔
2499
      STREAM_CHECK_NULL_GOTO(pLast, terrno);
466,563✔
2500
    }
2501

2502
    if (!node->needCalc) {
466,563✔
2503
      pWin->skey = pFirst->wstart;
297,834✔
2504
      pWin->ekey = pLast->wend;
297,834✔
2505
      *pValid = true;
297,709✔
2506
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
297,709✔
2507
        pWin->ekey--;
169,826✔
2508
      }
2509
    } else {
2510
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
168,604✔
2511
      memcpy(pTmp, pFirst, sizeof(*pTmp));
168,729✔
2512

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

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

2520
        if (*pValid) {
168,729✔
2521
          pWin->skey = skey;
168,729✔
2522
        }
2523
      }
2524
      pWin->ekey--;
168,729✔
2525
    }
2526
  } else {
2527
    if (!node->needCalc) {
143,984✔
2528
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
99,942✔
2529
      pWin->skey = pCurr->wstart;
99,942✔
2530
      pWin->ekey = pCurr->wend;
99,942✔
2531
      *pValid = true;
99,942✔
2532
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
99,942✔
2533
        pWin->ekey--;
51,186✔
2534
      }
2535
    } else {
2536
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, pWin, pValid, 3));
44,042✔
2537
      pWin->ekey--;
44,042✔
2538
    }
2539
  }
2540

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

2544
end:
4,748✔
2545

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

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

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

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

2579
  tsRsp->tsInfo = taosArrayInit(pResBlock->info.rows, sizeof(STsInfo));
360,451✔
2580
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
360,451✔
2581
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
360,451✔
2582
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
360,451✔
2583
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
1,001,780✔
2584
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
1,282,658✔
2585
      continue;
×
2586
    }
2587
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
641,329✔
2588
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
641,329✔
2589
    if (order == TSDB_ORDER_ASC) {
641,329✔
2590
      tsInfo->ts = INT64_MAX;
358,043✔
2591
    } else {
2592
      tsInfo->ts = INT64_MIN;
283,286✔
2593
    }
2594
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
641,329✔
2595
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
641,329✔
2596
      tsInfo->ts = ts;
358,043✔
2597
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
283,286✔
2598
      tsInfo->ts = ts;
283,286✔
2599
    }
2600
    tsInfo->gId = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
641,329✔
2601
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
641,329✔
2602
  }
2603

2604
end:
360,451✔
2605
  return code;
360,451✔
2606
}
2607

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

2613
  tsRsp->tsInfo = taosArrayInit(1, sizeof(STsInfo));
90,409✔
2614
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
89,937✔
2615
  STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
90,173✔
2616
  STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
90,173✔
2617
  if (order == TSDB_ORDER_ASC) {
90,173✔
2618
    tsInfo->ts = INT64_MAX;
71,462✔
2619
  } else {
2620
    tsInfo->ts = INT64_MIN;
18,711✔
2621
  }
2622

2623
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
89,700✔
2624
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
89,227✔
2625
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
195,127✔
2626
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
212,746✔
2627
      continue;
×
2628
    }
2629
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
106,373✔
2630
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
105,900✔
2631
      tsInfo->ts = ts;
71,225✔
2632
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
34,203✔
2633
      tsInfo->ts = ts;
18,711✔
2634
    }
2635
  }
2636
  int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, 0);
89,700✔
2637
  tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
90,173✔
2638
  ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
90,645✔
2639

2640
end:
30,953✔
2641
  return code;
90,645✔
2642
}
2643

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

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

2653
  SSHashObj*   uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
7,606✔
2654
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
7,606✔
2655
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
7,606✔
2656
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
7,606✔
2657
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
23,528✔
2658
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
31,844✔
2659
      continue;
×
2660
    }
2661
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
15,922✔
2662
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
15,922✔
2663
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &uid, LONG_BYTES, &ts, LONG_BYTES));
15,922✔
2664
  }
2665
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
7,606✔
2666
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
7,606✔
2667
  while (true) {
15,120✔
2668
    int32_t        pNum = 0;
22,726✔
2669
    int64_t        suid = 0;
22,726✔
2670
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
22,726✔
2671
    if(pNum == 0) break;
22,726✔
2672
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
15,709✔
2673
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
15,709✔
2674
    if (order == TSDB_ORDER_ASC) {
15,709✔
2675
      tsInfo->ts = INT64_MAX;
7,369✔
2676
    } else {
2677
      tsInfo->ts = INT64_MIN;
8,340✔
2678
    }
2679
    for (int32_t i = 0; i < pNum; i++) {
31,215✔
2680
      int64_t uid = pList[i].uid;
16,095✔
2681
      int64_t *ts = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
16,095✔
2682
      STREAM_CHECK_NULL_GOTO(ts, terrno);
16,095✔
2683
      if (order == TSDB_ORDER_ASC && *ts < tsInfo->ts) {
15,506✔
2684
        tsInfo->ts = *ts;
7,369✔
2685
      } else if (order == TSDB_ORDER_DESC && *ts > tsInfo->ts) {
8,137✔
2686
        tsInfo->ts = *ts;
7,751✔
2687
      }
2688
    }
2689
    int64_t uid = pList[0].uid;
15,120✔
2690
    tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
15,120✔
2691
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
15,120✔
2692
    taosMemoryFreeClear(pList);
15,120✔
2693
  }
2694

2695
end:
7,606✔
2696
  qStreamDestroyTableInfo(&tableInfo);
7,606✔
2697
  taosMemoryFreeClear(pList);
7,606✔
2698
  tSimpleHashCleanup(uidTsHash);
7,606✔
2699
  return code;
7,606✔
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) {
411,212✔
2705
  int32_t code = 0;
411,212✔
2706
  int32_t lino = 0;
411,212✔
2707

2708
  stDebug("%s getAllTs enter: pNum:%d suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" verRange:[%"PRId64",%"PRId64"]",
411,212✔
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,662,429✔
2713
    stDebug("%s getAllTs table[%d]: uid:%"PRId64, pTaskInner->idStr, i, pList[i].uid);
1,250,981✔
2714
  }
2715

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

2726
end:
410,975✔
2727
  stDebug("%s getAllTs done: code:%d pResBlock->info.rows:%"PRId64, pTaskInner->idStr, code, pResBlock ? pResBlock->info.rows : -1);
411,212✔
2728
  pTaskInner->storageApi->tsdReader.tsdDestroyFirstLastTsIter(pTaskInner->pReader);
411,212✔
2729
  pTaskInner->pReader = NULL;
409,660✔
2730
  return code;
410,549✔
2731
}
2732

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

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

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

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

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

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

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

2773
  SSDataBlock*  pResBlock = NULL;
241,211✔
2774

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

2782
  pTaskInner->options->suid = sStreamReaderInfo->suid;
208,457✔
2783
  ST_TASK_DLOG("vgId:%d %s calling getAllTs: suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" ver:%"PRId64,
208,457✔
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));
208,457✔
2787
  ST_TASK_DLOG("vgId:%d %s getAllTs done: pResBlock rows:%"PRId64, TD_VID(pVnode), __func__, pResBlock->info.rows);
208,203✔
2788
  int32_t order = pTaskInner->options->order;
208,630✔
2789
  if (pResBlock->info.rows == 0 && sStreamReaderInfo->groupByTbname) {
208,457✔
2790
    tsRsp->tsInfo = taosArrayInit(pNum, sizeof(STsInfo));
37,310✔
2791
    STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
37,310✔
2792
    for (int32_t i = 0; i < pNum; i++) {
137,771✔
2793
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
100,461✔
2794
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
100,461✔
2795
      tsInfo->gId = pList[i].uid;
100,461✔
2796
      tsInfo->ts = 0;
100,461✔
2797
      ST_TASK_DLOG("%s no data but return gId (uid):%" PRIu64 " for tbname partition", __func__, tsInfo->gId);
100,461✔
2798
    }
2799
    goto end;
37,310✔
2800
  }
2801

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

2804
  if (sStreamReaderInfo->groupByTbname) {
111,211✔
2805
    STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, order));
56,421✔
2806
  } else if (sStreamReaderInfo->partitionCols == NULL) {
54,790✔
2807
    STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order));
47,184✔
2808
  } else {
2809
    STREAM_CHECK_RET_GOTO(processTsOutPutAllGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
7,606✔
2810
  }                             
2811
end:
240,956✔
2812
  blockDataDestroy(pResBlock);
241,211✔
2813
  taosMemoryFreeClear(pList);
241,447✔
2814
  STREAM_PRINT_LOG_END_WITHID(code, lino);
241,211✔
2815
  return code;
241,211✔
2816
}
2817

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

2827
  int32_t        pNum = 0;
58,505✔
2828
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, gid, &pList, &pNum));
58,505✔
2829
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
58,505✔
2830
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
43,461✔
2831

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

2837
  STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order));
43,461✔
2838
end:
58,505✔
2839
  blockDataDestroy(pResBlock);
58,505✔
2840
  taosMemoryFreeClear(pList);
58,505✔
2841
  STREAM_PRINT_LOG_END_WITHID(code, lino);
58,505✔
2842
  return code;
58,505✔
2843
}
2844

2845
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
545,241✔
2846
                                  SStreamReaderTaskInner* pTaskInner) {
2847
  if (sStreamReaderInfo->isVtableStream) {
545,241✔
2848
    return processTsVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
304,030✔
2849
  }
2850

2851
  return processTsNonVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
241,211✔
2852
}
2853

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

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

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

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

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

2890
  void* pTask = sStreamReaderInfo->pTask;
298,001✔
2891

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

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

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

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

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

2921
  void* pTask = sStreamReaderInfo->pTask;
305,745✔
2922
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64
305,745✔
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;
305,745✔
2928

2929
  tsRsp.ver = pVnode->state.applied;
305,745✔
2930

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

2934
  if (req->firstTsReq.gid != 0) {
305,745✔
2935
    STREAM_CHECK_RET_GOTO(processTsOnce(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner, req->firstTsReq.gid));
58,505✔
2936
  } else {
2937
    STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
247,240✔
2938
  }
2939

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

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

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

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

2967
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
597,296✔
2968
    int32_t        pNum = 0;
597,296✔
2969
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbMetaReq.gid, &pList, &pNum));
597,296✔
2970
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, 
597,296✔
2971
                          sStreamReaderInfo->tsSchemas, true, NULL);
2972
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, pList, pNum, &sStreamReaderInfo->storageApi));
597,296✔
2973
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
597,296✔
2974
    
2975
    STREAM_CHECK_RET_GOTO(createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream));
597,296✔
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);
597,296✔
2984
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
597,296✔
2985
  bool hasNext = true;
597,296✔
2986
  while (true) {
305,774✔
2987
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
903,070✔
2988
    if (!hasNext) {
903,070✔
2989
      break;
597,296✔
2990
    }
2991
    pTaskInner->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
305,774✔
2992
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
305,774✔
2993

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

3003
    stDebug("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
305,649✔
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++;
305,774✔
3007
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
305,774✔
3008
      break;
×
3009
    }
3010
  }
3011

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

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

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

3036
  void* pTask = sStreamReaderInfo->pTask;
63,691✔
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, 
63,691✔
3038
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
3039
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
3040

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

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

3060
    SSDataBlock* pBlock = NULL;
63,691✔
3061
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
63,691✔
3062
    if (pBlock != NULL && pBlock->info.rows > 0) {
63,691✔
3063
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock,
63,691✔
3064
          0, pBlock->info.rows, 1));
3065
    }
3066
    
3067
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
63,691✔
3068
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
63,691✔
3069
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
63,691✔
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);
63,691✔
3075

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

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

3086
  releaseStreamTask(&pTaskInner);
63,691✔
3087
  return code;
63,691✔
3088
}
3089

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

3098
  void* pTask = sStreamReaderInfo->pTask;
3,116✔
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,116✔
3100
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
3101
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
3102

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

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

3118
    SSDataBlock* pBlock = NULL;
3,116✔
3119
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
3,116✔
3120
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
3,116✔
3121
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
3,116✔
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,116✔
3127
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
3,116✔
3128

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

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

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

3149
  SStreamReaderTaskInner* pTaskInner = NULL;
216,839✔
3150
  void* pTask = sStreamReaderInfo->pTask;
216,839✔
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);
216,839✔
3152
  
3153
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
216,839✔
3154

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

3169
  blockDataCleanup(pTaskInner->pResBlockDst);
118,659✔
3170
  bool hasNext = true;
118,659✔
3171
  int32_t totalRows = 0;
118,659✔
3172
    
3173
  pResList = taosArrayInit(4, POINTER_BYTES);
118,659✔
3174
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
118,659✔
3175
  while (1) {
121,399✔
3176
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
240,058✔
3177
    if (!hasNext) {
240,058✔
3178
      break;
68,059✔
3179
    }
3180
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
171,999✔
3181
    // pTaskInner->pResBlockDst->info.id.groupId = pTaskInner->pResBlock->info.id.groupId;
3182

3183
    SSDataBlock* pBlock = NULL;
171,999✔
3184
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
171,999✔
3185
    if (pBlock != NULL && pBlock->info.rows > 0) {
171,745✔
3186
      STREAM_CHECK_RET_GOTO(
171,745✔
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));
171,999✔
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);
171,999✔
3192
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pBlock, true, &pBlockTmp));
171,999✔
3193
    STREAM_CHECK_NULL_GOTO(taosArrayPush(pResList, &pBlockTmp), terrno);
171,999✔
3194
    totalRows += blockDataGetNumOfRows(pBlockTmp);
171,999✔
3195
    pBlockTmp = NULL;
171,999✔
3196

3197
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
171,999✔
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
171,999✔
3201
      break;
50,600✔
3202
    }
3203
  }
3204

3205
  STREAM_CHECK_RET_GOTO(buildArrayRsp(pResList, &buf, &size));
118,659✔
3206
  if (!hasNext) {
118,659✔
3207
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
68,059✔
3208
  }
3209

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

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

3229

3230
  void* pTask = sStreamReaderInfo->pTask;
6,593,656✔
3231
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
6,593,656✔
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);
6,593,872✔
3235

3236
  SStreamReaderTaskInner* pTaskInner = NULL;
6,593,656✔
3237
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
6,593,656✔
3238

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

3246
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
6,587,400✔
3247
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
6,590,856✔
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);
6,591,936✔
3256
  bool hasNext = true;
6,588,696✔
3257
  while (1) {
806,254✔
3258
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
7,397,110✔
3259
    if (!hasNext) {
7,393,052✔
3260
      break;
6,587,400✔
3261
    }
3262
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
805,652✔
3263

3264
    SSDataBlock* pBlock = NULL;
806,254✔
3265
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
806,254✔
3266
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
806,061✔
3267
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
805,675✔
3268
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
806,254✔
3269
      break;
×
3270
    }
3271
  }
3272

3273
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
6,587,616✔
3274
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
6,589,992✔
3275
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
6,586,536✔
3276
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
6,590,824✔
3277
  printDataBlock(pBlockRes, __func__, "tsdb_calc_data", ((SStreamTask*)pTask)->streamId);
6,588,040✔
3278
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
6,586,320✔
3279
  printDataBlock(pBlockRes, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
6,593,656✔
3280

3281
  if (!hasNext) {
6,593,656✔
3282
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
6,593,656✔
3283
  }
3284

3285
end:
6,592,159✔
3286
  STREAM_PRINT_LOG_END_WITHID(code, lino);
6,591,365✔
3287
  SRpcMsg rsp = {
6,593,525✔
3288
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3289
  tmsgSendRsp(&rsp);
6,593,224✔
3290
  blockDataDestroy(pBlockRes);
6,592,360✔
3291
  taosMemoryFree(pList);
6,592,576✔
3292
  return code;
6,593,224✔
3293
}
3294

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

3312
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
442,753✔
3313
    // sort cid and build slotIdList
3314
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
442,753✔
3315
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
442,753✔
3316
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
442,753✔
3317
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
442,753✔
3318
    taosArraySort(sortedCid, sortCid);
442,753✔
3319
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
1,729,635✔
3320
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
1,287,121✔
3321
      STREAM_CHECK_NULL_GOTO(cid, terrno);
1,287,121✔
3322
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
2,541,779✔
3323
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
2,541,779✔
3324
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
2,541,540✔
3325
        if (*cid == *cidSorted) {
2,541,540✔
3326
          slotIdList[j] = i;
1,286,882✔
3327
          break;
1,286,882✔
3328
        }
3329
      }
3330
    }
3331

3332
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, req->tsdbDataReq.uid, &schemas, &sStreamReaderInfo->storageApi));
442,753✔
3333
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
438,858✔
3334
    STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlockRes));
438,858✔
3335

3336
    taosArraySort(schemas, sortSSchema);
438,858✔
3337
    BUILD_OPTION(options, req->tsdbDataReq.suid, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
438,858✔
3338
                    req->tsdbDataReq.ekey, schemas, true, &slotIdList);
3339
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid, .groupId = 0};
438,858✔
3340
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, pBlockRes, &keyInfo, 1, &sStreamReaderInfo->storageApi));
438,858✔
3341
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
438,858✔
3342
    pTaskInner->pResBlockDst = pBlockRes;
438,858✔
3343
    pBlockRes = NULL;
438,858✔
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);
438,858✔
3352
  bool hasNext = true;
438,858✔
3353
  while (1) {
438,858✔
3354
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
877,716✔
3355
    if (!hasNext) {
877,716✔
3356
      break;
438,858✔
3357
    }
3358

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

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

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

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

3396
  if (sStreamReaderInfo->metaBlock == NULL) {
19,384,205✔
3397
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
203,170✔
3398
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
203,170✔
3399
  }
3400
  blockDataEmpty(sStreamReaderInfo->metaBlock);
19,384,205✔
3401
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
19,383,397✔
3402
  resultRsp.ver = req->walMetaNewReq.lastVer;
19,383,397✔
3403
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
19,383,605✔
3404

3405
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
19,383,456✔
3406
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
19,384,444✔
3407
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
447,501✔
3408
  buf = rpcMallocCont(size);
447,501✔
3409
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
447,501✔
3410
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
447,501✔
3411
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
447,501✔
3412
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
447,501✔
3413

3414
end:
19,384,444✔
3415
  if (code == 0 && resultRsp.totalRows == 0) {
19,384,225✔
3416
    code = TSDB_CODE_STREAM_NO_DATA;
18,936,724✔
3417
    size = sizeof(int64_t) * 2;
18,936,724✔
3418
    buf = rpcMallocCont(size);
18,936,724✔
3419
    *(int64_t*)buf = resultRsp.ver;
18,936,943✔
3420
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
18,936,943✔
3421
  }
3422
  SRpcMsg rsp = {
19,384,444✔
3423
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3424
  tmsgSendRsp(&rsp);
19,384,444✔
3425
  if (code == TSDB_CODE_STREAM_NO_DATA){
19,382,281✔
3426
    code = 0;
18,934,372✔
3427
  }
3428
  STREAM_PRINT_LOG_END_WITHID(code, lino);
19,382,281✔
3429
  blockDataDestroy(resultRsp.deleteBlock);
19,385,074✔
3430
  blockDataDestroy(resultRsp.tableBlock);
19,384,444✔
3431

3432
  return code;
19,384,444✔
3433
}
3434
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
3,763,008✔
3435
  int32_t      code = 0;
3,763,008✔
3436
  int32_t      lino = 0;
3,763,008✔
3437
  void*        buf = NULL;
3,763,008✔
3438
  size_t       size = 0;
3,763,008✔
3439
  SSTriggerWalNewRsp resultRsp = {0};
3,763,008✔
3440
  
3441
  void* pTask = sStreamReaderInfo->pTask;
3,764,141✔
3442
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaDataNewReq.lastVer);
3,763,260✔
3443

3444
  if (sStreamReaderInfo->metaBlock == NULL) {
3,763,668✔
3445
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
104,407✔
3446
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
104,407✔
3447
  }
3448

3449
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
3,763,669✔
3450
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
3,764,141✔
3451
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
3,761,636✔
3452
  resultRsp.checkAlter = true;
3,761,525✔
3453
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
3,761,525✔
3454
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
3,763,081✔
3455

3456
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp));
3,763,081✔
3457

3458
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
3,761,804✔
3459
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
133,613✔
3460
  buf = rpcMallocCont(size);
134,558✔
3461
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
134,558✔
3462
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
134,558✔
3463
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
134,368✔
3464
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
134,368✔
3465
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
134,368✔
3466
  printIndexHash(resultRsp.indexHash, pTask);
134,368✔
3467

3468
end:
3,762,720✔
3469
  if (resultRsp.totalRows == 0) {
3,763,429✔
3470
    code = TSDB_CODE_STREAM_NO_DATA;
3,628,426✔
3471
    size = sizeof(int64_t) * 2;
3,628,426✔
3472
    buf = rpcMallocCont(size);
3,628,426✔
3473
    *(int64_t*)buf = resultRsp.ver;
3,627,248✔
3474
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
3,627,248✔
3475
  }
3476
  SRpcMsg rsp = {
3,762,488✔
3477
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3478
  tmsgSendRsp(&rsp);
3,763,432✔
3479
  if (code == TSDB_CODE_STREAM_NO_DATA){
3,763,183✔
3480
    code = 0;
3,628,665✔
3481
  }
3482
  blockDataDestroy(resultRsp.dataBlock);
3,763,183✔
3483
  blockDataDestroy(resultRsp.deleteBlock);
3,763,668✔
3484
  blockDataDestroy(resultRsp.tableBlock);
3,763,904✔
3485
  tSimpleHashCleanup(resultRsp.indexHash);
3,763,433✔
3486

3487
  STREAM_PRINT_LOG_END_WITHID(code, lino);
3,760,362✔
3488

3489
  return code;
3,760,839✔
3490
}
3491

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

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

3502
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
13,086,148✔
3503
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
13,083,032✔
3504
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
13,080,456✔
3505
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
13,080,456✔
3506
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
13,080,213✔
3507

3508
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
13,080,213✔
3509
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
13,084,891✔
3510

3511
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
13,084,891✔
3512

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

3519
end:
13,084,891✔
3520
  if (resultRsp.totalRows == 0) {
13,084,413✔
3521
    buf = rpcMallocCont(sizeof(int64_t));
12,867,058✔
3522
    *(int64_t *)buf = resultRsp.ver;
12,867,058✔
3523
    size = sizeof(int64_t);
12,867,536✔
3524
    code = TSDB_CODE_STREAM_NO_DATA;
12,867,536✔
3525
  }
3526
  SRpcMsg rsp = {
13,084,891✔
3527
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3528
  tmsgSendRsp(&rsp);
13,084,891✔
3529
  if (code == TSDB_CODE_STREAM_NO_DATA){
13,081,474✔
3530
    code = 0;
12,866,456✔
3531
  }
3532

3533
  blockDataDestroy(resultRsp.dataBlock);
13,081,474✔
3534
  blockDataDestroy(resultRsp.deleteBlock);
13,080,996✔
3535
  blockDataDestroy(resultRsp.tableBlock);
13,080,996✔
3536
  tSimpleHashCleanup(resultRsp.indexHash);
13,084,112✔
3537
  tSimpleHashCleanup(resultRsp.uidHash);
13,081,552✔
3538
  STREAM_PRINT_LOG_END_WITHID(code, lino);
13,081,873✔
3539

3540
  return code;
13,085,991✔
3541
}
3542

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

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

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

3566
  if (!sStreamReaderInfo->isVtableStream){
249,180✔
3567
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
148,774✔
3568
  
3569
    blockDataTransform(pBlock2, resultRsp.dataBlock);
148,774✔
3570
    blockDataDestroy(resultRsp.dataBlock);
148,774✔
3571
    resultRsp.dataBlock = pBlock2;
148,774✔
3572
    pBlock2 = NULL;
148,774✔
3573
  }
3574

3575
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
249,180✔
3576
  buf = rpcMallocCont(size);
248,692✔
3577
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
248,928✔
3578
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
249,180✔
3579
  printIndexHash(resultRsp.indexHash, pTask);
248,692✔
3580

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

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

3604
  return code;
1,100,251✔
3605
}
3606

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

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

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

3640
  return code;
314,080✔
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) {
189,437✔
3644
  int32_t              code = 0;
189,437✔
3645
  int32_t              lino = 0;
189,437✔
3646
  void* pTask = sStreamReaderInfo->pTask;
189,437✔
3647

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

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

3655
  code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(metaReader, ver, uid);
189,437✔
3656
  if (code != 0) {
189,437✔
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) {
189,437✔
3661
    vTable->cols.nCols = metaReader->me.colRef.nCols;
10,959✔
3662
    vTable->cols.version = metaReader->me.colRef.version;
10,959✔
3663
    vTable->cols.pColRef = taosMemoryCalloc(metaReader->me.colRef.nCols, sizeof(SColRef));
10,959✔
3664
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
10,959✔
3665
    for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
65,754✔
3666
      memcpy(vTable->cols.pColRef + j, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
54,795✔
3667
    }
3668
  } else {
3669
    vTable->cols.nCols = taosArrayGetSize(cids);
178,478✔
3670
    vTable->cols.version = metaReader->me.colRef.version;
178,478✔
3671
    vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
178,478✔
3672
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
178,478✔
3673
    for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
679,290✔
3674
      for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
2,109,909✔
3675
        if (metaReader->me.colRef.pColRef[j].hasRef &&
1,930,723✔
3676
            metaReader->me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
1,425,472✔
3677
          memcpy(vTable->cols.pColRef + i, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
321,626✔
3678
          break;
321,626✔
3679
        }
3680
      }
3681
    }
3682
  }
3683
  tDecoderClear(&metaReader->coder);
189,437✔
3684

3685
end:
189,437✔
3686
  return code;
189,437✔
3687
}
3688

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

3695

3696
  pTableListArray = qStreamGetTableArrayList(sStreamReaderInfo);
76,376✔
3697
  STREAM_CHECK_NULL_GOTO(pTableListArray, terrno);
76,376✔
3698

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

3702
  for (size_t i = 0; i < taosArrayGetSize(pTableListArray); i++) {
265,813✔
3703
    SStreamTableKeyInfo* pKeyInfo = taosArrayGetP(pTableListArray, i);
189,437✔
3704
    if (pKeyInfo == NULL || pKeyInfo->markedDeleted) {
189,437✔
3705
      continue;
×
3706
    }
3707
    code = setVtableInfo(pVnode, vTableInfo->infos, cids, pKeyInfo->uid, pKeyInfo->groupId, ver, metaReader, sStreamReaderInfo);
189,437✔
3708
    if (code != 0) {
189,437✔
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:
76,376✔
3716
  taosArrayDestroyP(pTableListArray, taosMemFree);
76,376✔
3717
  return code;
76,376✔
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) {
76,168✔
3752
  int32_t              code = 0;
76,168✔
3753
  int32_t              lino = 0;
76,168✔
3754
  void*                buf = NULL;
76,168✔
3755
  size_t               size = 0;
76,376✔
3756
  SStreamMsgVTableInfo vTableInfo = {0};
76,376✔
3757
  SMetaReader          metaReader = {0};
76,376✔
3758

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

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

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

3770
  if (req->virTableInfoReq.fetchAllTable || req->virTableInfoReq.uids == NULL || taosArrayGetSize(req->virTableInfoReq.uids) == 0) {
76,376✔
3771
    STREAM_CHECK_RET_GOTO(getAllVinfo(pVnode, &vTableInfo, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
76,376✔
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));
76,376✔
3776
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
76,376✔
3777

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

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

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

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

3802
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
135,944✔
3803

3804
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
135,944✔
3805

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

3821
    SSchemaWrapper* sSchemaWrapper = NULL;
365,254✔
3822
    if (metaReader.me.type == TD_CHILD_TABLE) {
365,254✔
3823
      int64_t suid = metaReader.me.ctbEntry.suid;
363,376✔
3824
      vTableInfo->suid = suid;
363,376✔
3825
      tDecoderClear(&metaReader.coder);
363,376✔
3826
      STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->origTableInfoReq.ver, suid));
363,376✔
3827
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
363,376✔
3828
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
1,878✔
3829
      vTableInfo->suid = 0;
1,878✔
3830
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
1,878✔
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,251,110✔
3836
      SSchema* s = sSchemaWrapper->pSchema + j;
1,251,110✔
3837
      if (strcmp(s->name, oInfo->refColName) == 0) {
1,251,110✔
3838
        vTableInfo->cid = s->colId;
365,254✔
3839
        break;
365,254✔
3840
      }
3841
    }
3842
    if (vTableInfo->cid == 0) {
365,254✔
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);
365,254✔
3847
  }
3848

3849
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
135,944✔
3850

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

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

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

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

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

3879
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
899,496✔
3880

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

3896
    STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReaderStable, req->virTablePseudoColReq.ver, suid));
897,372✔
3897
    SSchemaWrapper*  sSchemaWrapper = &metaReaderStable.me.stbEntry.schemaTag;
897,372✔
3898
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
2,357,894✔
3899
      col_id_t* id = taosArrayGet(cols, i);
1,460,522✔
3900
      STREAM_CHECK_NULL_GOTO(id, terrno);
1,460,522✔
3901
      if (*id == -1) {
1,460,522✔
3902
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
891,140✔
3903
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
891,140✔
3904
        continue;
891,140✔
3905
      }
3906
      size_t j = 0;
569,382✔
3907
      for (; j < sSchemaWrapper->nCols; j++) {
1,096,831✔
3908
        SSchema* s = sSchemaWrapper->pSchema + j;
1,096,831✔
3909
        if (s->colId == *id) {
1,096,831✔
3910
          SColumnInfoData idata = createColumnInfoData(s->type, s->bytes, s->colId);
569,382✔
3911
          STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
569,382✔
3912
          break;
569,382✔
3913
        }
3914
      }
3915
      if (j == sSchemaWrapper->nCols) {
569,382✔
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));
897,372✔
3921
    pBlock->info.rows = 1;
897,372✔
3922
    
3923
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
2,357,894✔
3924
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
1,460,522✔
3925
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
1,460,522✔
3926

3927
      if (pDst->info.colId == -1) {
1,460,522✔
3928
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
891,140✔
3929
        continue;
891,140✔
3930
      }
3931
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
569,382✔
3932
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
3933
        continue;
×
3934
      }
3935

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

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

3947
      STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data,
569,382✔
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) &&
569,382✔
3951
          (data != NULL)) {
3952
        taosMemoryFree(data);
521,974✔
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);
899,496✔
3962
  printDataBlock(pBlock, __func__, "", streamId);
899,496✔
3963
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
899,496✔
3964

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

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

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

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

4000
  void* pTask = sStreamReaderCalcInfo->pTask;
7,795,289✔
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,
7,795,289✔
4002
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
4003

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

4024
    initStorageAPI(&handle.api);
4,780,442✔
4025
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
4,780,317✔
4026
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
3,821,869✔
4027
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
3,461,892✔
4028
      if (node != NULL) {
3,462,118✔
4029
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, false));
532,007✔
4030
      } else {
4031
        ST_TASK_DLOG("vgId:%d %s no scan time range node", TD_VID(pVnode), __func__);
2,930,111✔
4032
      }
4033

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

4042
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
4,780,216✔
4043
    sStreamReaderCalcInfo->rtInfo.funcInfo.hasPlaceHolder = sStreamReaderCalcInfo->hasPlaceHolder;
4,780,442✔
4044
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
4,780,216✔
4045

4046
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
4,780,216✔
4047
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
684,249✔
4048
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
684,475✔
4049
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
4050
                                                    req.taskId));
4051
      STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
684,475✔
4052
    } else {
4053
      STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle));
4,095,967✔
4054
    }
4055

4056
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
4,780,167✔
4057
  }
4058

4059
  if (req.pOpParam != NULL) {
7,795,014✔
4060
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, (void*)req.pOpParam);
407,853✔
4061
  }
4062

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

4068
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
18,469,042✔
4069
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
10,675,169✔
4070
    if (pBlock == NULL) continue;
10,675,169✔
4071
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
10,675,169✔
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:
7,794,778✔
4081
  code = streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision);
7,795,289✔
4082

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

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

4130
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo *pInfo) {
55,368,898✔
4131
  int32_t                   code = 0;
55,368,898✔
4132
  int32_t                   lino = 0;
55,368,898✔
4133
  SSTriggerPullRequestUnion req = {0};
55,368,898✔
4134
  void*                     taskAddr = NULL;
55,370,041✔
4135
  bool                      sendRsp = false;
55,365,595✔
4136

4137
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
55,365,595✔
4138
  if (!syncIsReadyForRead(pVnode->sync)) {
55,367,931✔
4139
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
133,304✔
4140
    return 0;
133,304✔
4141
  }
4142

4143
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
55,235,322✔
4144
    return vnodeProcessStreamFetchMsg(pVnode, pMsg, pInfo);
7,795,289✔
4145
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
47,439,911✔
4146
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
47,441,594✔
4147
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
47,441,592✔
4148
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
47,441,049✔
4149
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64 ", applied:%" PRIx64,
47,437,332✔
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);
47,439,017✔
4152
    STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
47,440,216✔
4153
    STREAM_CHECK_RET_GOTO(initTableList(sStreamReaderInfo, pVnode));
47,415,527✔
4154
    sendRsp = true;
47,416,191✔
4155
    switch (req.base.type) {
47,416,191✔
4156
      case STRIGGER_PULL_SET_TABLE:
135,944✔
4157
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo));
135,944✔
4158
        break;
135,944✔
4159
      case STRIGGER_PULL_LAST_TS:
298,001✔
4160
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
298,001✔
4161
        break;
298,001✔
4162
      case STRIGGER_PULL_FIRST_TS:
305,745✔
4163
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
305,745✔
4164
        break;
305,745✔
4165
      case STRIGGER_PULL_TSDB_META:
597,296✔
4166
      case STRIGGER_PULL_TSDB_META_NEXT:
4167
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo));
597,296✔
4168
        break;
597,296✔
4169
      case STRIGGER_PULL_TSDB_TS_DATA:
66,807✔
4170
        if (sStreamReaderInfo->isVtableStream) {
66,807✔
4171
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo));
3,116✔
4172
        } else {
4173
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo));
63,691✔
4174
        }
4175
        break;
66,807✔
4176
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
216,839✔
4177
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
4178
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
216,839✔
4179
        break;
118,659✔
4180
      case STRIGGER_PULL_TSDB_CALC_DATA:
6,593,440✔
4181
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
4182
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
6,593,440✔
4183
        break;
6,592,144✔
4184
      case STRIGGER_PULL_TSDB_DATA:
442,753✔
4185
      case STRIGGER_PULL_TSDB_DATA_NEXT:
4186
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
442,753✔
4187
        break;
438,858✔
4188
      case STRIGGER_PULL_GROUP_COL_VALUE:
314,080✔
4189
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo));
314,080✔
4190
        break;
314,080✔
4191
      case STRIGGER_PULL_VTABLE_INFO:
76,376✔
4192
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
76,376✔
4193
        break;
76,376✔
4194
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
899,496✔
4195
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
899,496✔
4196
        break;
899,496✔
4197
      case STRIGGER_PULL_OTABLE_INFO:
135,944✔
4198
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
135,944✔
4199
        break;
135,944✔
4200
      case STRIGGER_PULL_WAL_META_NEW:
19,383,966✔
4201
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
19,383,966✔
4202
        break;
19,384,271✔
4203
      case STRIGGER_PULL_WAL_DATA_NEW:
13,085,369✔
4204
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
13,085,369✔
4205
        break;
13,084,891✔
4206
      case STRIGGER_PULL_WAL_META_DATA_NEW:
3,763,905✔
4207
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
3,763,905✔
4208
        break;
3,763,223✔
4209
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
1,100,251✔
4210
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
1,100,251✔
4211
        break;
1,100,251✔
UNCOV
4212
      default:
×
UNCOV
4213
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
×
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);
236✔
4219
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
4220
  }
4221
end:
47,428,353✔
4222

4223
  streamReleaseTask(taskAddr);
47,438,377✔
4224

4225
  tDestroySTriggerPullRequest(&req);
47,439,494✔
4226
  STREAM_PRINT_LOG_END(code, lino);
47,435,795✔
4227
  if (!sendRsp) {
47,439,697✔
4228
    SRpcMsg rsp = {
49,378✔
4229
      .code = code,
4230
      .pCont = pMsg->info.rsp,
24,689✔
4231
      .contLen = pMsg->info.rspLen,
24,689✔
4232
      .info = pMsg->info,
4233
    };
4234
    tmsgSendRsp(&rsp);
24,689✔
4235
  }
4236
  return code;
47,439,458✔
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