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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

69.36
/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 "nodes.h"
19
#include "osMemPool.h"
20
#include "osMemory.h"
21
#include "scalar.h"
22
#include "streamReader.h"
23
#include "taosdef.h"
24
#include "tarray.h"
25
#include "tcommon.h"
26
#include "tdatablock.h"
27
#include "tdb.h"
28
#include "tdef.h"
29
#include "tencode.h"
30
#include "tglobal.h"
31
#include "thash.h"
32
#include "tlist.h"
33
#include "tmsg.h"
34
#include "tsimplehash.h"
35
#include "vnd.h"
36
#include "vnode.h"
37
#include "vnodeInt.h"
38

39
#define BUILD_OPTION(options, sStreamReaderInfo, _ver, _order, startTime, endTime, _schemas, _isSchema, _scanMode, \
40
                     _gid, _initReader, _mapInfo)                                                                        \
41
  SStreamTriggerReaderTaskInnerOptions options = {.suid = (_mapInfo == NULL ? sStreamReaderInfo->suid : 0),              \
42
                                                  .ver = _ver,                                                     \
43
                                                  .order = _order,                                                 \
44
                                                  .twindows = {.skey = startTime, .ekey = endTime},                \
45
                                                  .sStreamReaderInfo = sStreamReaderInfo,                     \
46
                                                  .schemas = _schemas,                                             \
47
                                                  .isSchema = _isSchema,                                           \
48
                                                  .scanMode = _scanMode,                                           \
49
                                                  .gid = _gid,                                                     \
50
                                                  .initReader = _initReader,                                       \
51
                                                  .mapInfo = _mapInfo};
52

53
typedef struct WalMetaResult {
54
  uint64_t    id;
55
  int64_t     skey;
56
  int64_t     ekey;
57
} WalMetaResult;
58

59
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
30,075✔
60

61
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas);
62

63
int32_t sortCid(const void *lp, const void *rp) {
1,502✔
64
  int16_t* c1 = (int16_t*)lp;
1,502✔
65
  int16_t* c2 = (int16_t*)rp;
1,502✔
66

67
  if (*c1 < *c2) {
1,502✔
68
    return -1;
1,497✔
69
  } else if (*c1 > *c2) {
5!
70
    return 1;
5✔
71
  }
72

73
  return 0;
×
74
}
75

76
int32_t sortSSchema(const void *lp, const void *rp) {
1,498✔
77
  SSchema* c1 = (SSchema*)lp;
1,498✔
78
  SSchema* c2 = (SSchema*)rp;
1,498✔
79

80
  if (c1->colId < c2->colId) {
1,498✔
81
    return -1;
1,493✔
82
  } else if (c1->colId > c2->colId) {
5!
83
    return 1;
5✔
84
  }
85

86
  return 0;
×
87
}
88

89
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
66,455✔
90
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
66,455✔
91
  if (pSrc == NULL) {
66,464✔
92
    return terrno;
8✔
93
  }
94

95
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
66,456✔
96
  return 0;
66,456✔
97
}
98

99
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
37,767✔
100
  int32_t code = pTask->api.tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
37,767✔
101
  if (code != TSDB_CODE_SUCCESS) {
37,765!
102
    pTask->api.tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
103
  }
104

105
  return code;
37,766✔
106
}
107

108
static int32_t getTableData(SStreamReaderTaskInner* pTask, SSDataBlock** ppRes) {
3,758✔
109
  return pTask->api.tsdReader.tsdReaderRetrieveDataBlock(pTask->pReader, ppRes, NULL);
3,758✔
110
}
111

112
static int32_t buildOTableInfoRsp(const SSTriggerOrigTableInfoRsp* rsp, void** data, size_t* size) {
240✔
113
  int32_t code = 0;
240✔
114
  int32_t lino = 0;
240✔
115
  void*   buf = NULL;
240✔
116
  int32_t len = tSerializeSTriggerOrigTableInfoRsp(NULL, 0, rsp);
240✔
117
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
240!
118
  buf = rpcMallocCont(len);
240✔
119
  STREAM_CHECK_NULL_GOTO(buf, terrno);
240!
120
  int32_t actLen = tSerializeSTriggerOrigTableInfoRsp(buf, len, rsp);
240✔
121
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
240!
122
  *data = buf;
240✔
123
  *size = len;
240✔
124
  buf = NULL;
240✔
125
end:
240✔
126
  rpcFreeCont(buf);
240✔
127
  return code;
240✔
128
}
129

130
static bool needRefreshTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
7,384✔
131
  if (sStreamReaderInfo->isVtableStream) {
7,384✔
132
    int64_t id[2] = {suid, uid};
5,917✔
133
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id)) == NULL) {
5,917!
134
      return true;
5,909✔
135
    }
136
  } else {
137
    if (tableType != TD_CHILD_TABLE) {
1,467✔
138
      return false;
498✔
139
    }
140
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
969✔
141
        suid == sStreamReaderInfo->suid && 
596✔
142
        qStreamGetGroupId(sStreamReaderInfo->tableList, uid) == -1) {
23✔
143
      return true;
9✔
144
    }
145
  }
146
  return false;
968✔
147
}
148

149
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc){
92,039✔
150
  if (sStreamReaderInfo->isVtableStream) {
92,039✔
151
    int64_t tmp[2] = {suid, uid};
67,218✔
152
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) == NULL) {
67,218✔
153
      return false;
32,830✔
154
    }
155
    *id = uid;
34,465✔
156
  } else {
157
    if (sStreamReaderInfo->tableList == NULL) return false;
24,821!
158

159
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
24,821✔
160
      if (suid != sStreamReaderInfo->suid) return false;
16,749✔
161
      if (sStreamReaderInfo->pTagCond == NULL) {
13,057✔
162
        if (sStreamReaderInfo->partitionCols == NULL){
11,533✔
163
          *id = 0;
40✔
164
        } else if (sStreamReaderInfo->groupByTbname){
11,493✔
165
          *id= uid;
10,798✔
166
        } else {
167
          *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
695✔
168
          if (*id == -1) return false;
704!
169
        }
170
      } else {
171
        //*id= uid;
172
        *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
1,524✔
173
        if (*id == -1) return false;
1,671✔
174
      }
175
    } else {
176
      *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
8,072✔
177
      if(*id == -1) *id = uid;
8,510✔
178
      return uid == sStreamReaderInfo->uid;
8,510✔
179
    }
180
  }
181
  return true;
46,980✔
182
}
183

184
static int32_t generateTablistForStreamReader(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, bool isHistory) {
13,095✔
185
  int32_t                   code = 0;
13,095✔
186
  int32_t                   lino = 0;
13,095✔
187
  SNodeList* groupNew = NULL;                                      
13,095✔
188
  STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
13,095!
189

190
  SStorageAPI api = {0};
13,096✔
191
  initStorageAPI(&api);
13,096✔
192
  code = qStreamCreateTableListForReader(pVnode, sStreamReaderInfo->suid, sStreamReaderInfo->uid, sStreamReaderInfo->tableType, groupNew,
13,095✔
193
                                         true, sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond, &api, 
194
                                         isHistory ? &sStreamReaderInfo->historyTableList : &sStreamReaderInfo->tableList,
195
                                         isHistory ? NULL : sStreamReaderInfo->groupIdMap);
196
  end:
13,097✔
197
  nodesDestroyList(groupNew);
13,097✔
198
  STREAM_PRINT_LOG_END(code, lino);
13,097!
199
  return code;
13,098✔
200
}
201

202
static int32_t buildVTableInfoRsp(const SStreamMsgVTableInfo* rsp, void** data, size_t* size) {
791✔
203
  int32_t code = 0;
791✔
204
  int32_t lino = 0;
791✔
205
  void*   buf = NULL;
791✔
206
  int32_t len = tSerializeSStreamMsgVTableInfo(NULL, 0, rsp);
791✔
207
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
791!
208
  buf = rpcMallocCont(len);
791✔
209
  STREAM_CHECK_NULL_GOTO(buf, terrno);
790!
210
  int32_t actLen = tSerializeSStreamMsgVTableInfo(buf, len, rsp);
790✔
211
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
791!
212
  *data = buf;
791✔
213
  *size = len;
791✔
214
  buf = NULL;
791✔
215
end:
791✔
216
  rpcFreeCont(buf);
791✔
217
  return code;
791✔
218
}
219

220
static int32_t buildTsRsp(const SStreamTsResponse* tsRsp, void** data, size_t* size) {
1,057✔
221
  int32_t code = 0;
1,057✔
222
  int32_t lino = 0;
1,057✔
223
  void*   buf = NULL;
1,057✔
224
  int32_t len = tSerializeSStreamTsResponse(NULL, 0, tsRsp);
1,057✔
225
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
1,057!
226
  buf = rpcMallocCont(len);
1,057✔
227
  STREAM_CHECK_NULL_GOTO(buf, terrno);
1,057!
228
  int32_t actLen = tSerializeSStreamTsResponse(buf, len, tsRsp);
1,057✔
229
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
1,057!
230
  *data = buf;
1,057✔
231
  *size = len;
1,057✔
232
  buf = NULL;
1,057✔
233
end:
1,057✔
234
  rpcFreeCont(buf);
1,057✔
235
  return code;
1,057✔
236
}
237

238

239
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
31,960✔
240
  int32_t code = 0;
31,960✔
241
  int32_t lino = 0;
31,960✔
242
  void*   buf = NULL;
31,960✔
243
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
31,960!
244
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
3,883✔
245
  buf = rpcMallocCont(dataEncodeSize);
3,883✔
246
  STREAM_CHECK_NULL_GOTO(buf, terrno);
3,883!
247
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
3,883✔
248
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
3,883!
249
  *data = buf;
3,883✔
250
  *size = dataEncodeSize;
3,883✔
251
  buf = NULL;
3,883✔
252
end:
31,960✔
253
  rpcFreeCont(buf);
31,960✔
254
  return code;
31,961✔
255
}
256

257
static int32_t resetTsdbReader(SStreamReaderTaskInner* pTask) {
2,120✔
258
  int32_t        pNum = 1;
2,120✔
259
  STableKeyInfo* pList = NULL;
2,120✔
260
  int32_t        code = 0;
2,120✔
261
  int32_t        lino = 0;
2,120✔
262
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(pTask->pTableList, pTask->currentGroupIndex, &pList, &pNum));
2,120!
263
  if (pList == NULL || pNum == 0) {
2,120!
264
    code = TSDB_CODE_INVALID_PARA;
×
265
    goto end;
×
266
  }
267
  STREAM_CHECK_RET_GOTO(pTask->api.tsdReader.tsdSetQueryTableList(pTask->pReader, pList, pNum));
2,120!
268

269
  cleanupQueryTableDataCond(&pTask->cond);
2,120✔
270
  uint64_t suid = pTask->options.sStreamReaderInfo->isVtableStream ? pList->groupId : pTask->options.suid;
2,120✔
271
  STREAM_CHECK_RET_GOTO(qStreamInitQueryTableDataCond(&pTask->cond, pTask->options.order, pTask->options.schemas, true,
2,120!
272
                                                      pTask->options.twindows, suid, pTask->options.ver, NULL));
273
  STREAM_CHECK_RET_GOTO(pTask->api.tsdReader.tsdReaderResetStatus(pTask->pReader, &pTask->cond));
2,120!
274

275
end:
2,120✔
276
  STREAM_PRINT_LOG_END(code, lino);
2,120!
277
  return code;
2,120✔
278
}
279

280
static int32_t buildWalMetaBlock(SSDataBlock* pBlock, int8_t type, int64_t id, bool isVTable, int64_t uid,
×
281
                                 int64_t skey, int64_t ekey, int64_t ver, int64_t rows) {
282
  int32_t code = 0;
×
283
  int32_t lino = 0;
×
284
  int32_t index = 0;
×
285
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
×
286
  if (!isVTable) {
×
287
    STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
×
288
  }
289
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &uid));
×
290
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
×
291
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
×
292
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
×
293
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &rows));
×
294

295
end:
×
296
  // STREAM_PRINT_LOG_END(code, lino)
297
  return code;
×
298
}
299

300
static int32_t buildWalMetaBlockNew(SSDataBlock* pBlock, int64_t id, int64_t skey, int64_t ekey, int64_t ver) {
16,281✔
301
  int32_t code = 0;
16,281✔
302
  int32_t lino = 0;
16,281✔
303
  int32_t index = 0;
16,281✔
304
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
16,281!
305
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
16,271!
306
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
16,240!
307
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
16,232!
308

309
end:
16,231✔
310
  return code;
16,231✔
311
}
312

313
static int32_t buildDropTableBlock(SSDataBlock* pBlock, int64_t id, int64_t ver) {
1✔
314
  int32_t code = 0;
1✔
315
  int32_t lino = 0;
1✔
316
  int32_t index = 0;
1✔
317
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
1!
318
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
1!
319

320
end:
1✔
321
  return code;
1✔
322
}
323

324
static void buildTSchema(STSchema* pTSchema, int32_t ver, col_id_t colId, int8_t type, int32_t bytes) {
×
325
  pTSchema->numOfCols = 1;
×
326
  pTSchema->version = ver;
×
327
  pTSchema->columns[0].colId = colId;
×
328
  pTSchema->columns[0].type = type;
×
329
  pTSchema->columns[0].bytes = bytes;
×
330
}
×
331

332
static int32_t scanDeleteDataNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
119✔
333
                              int64_t ver) {
334
  int32_t    code = 0;
119✔
335
  int32_t    lino = 0;
119✔
336
  SDecoder   decoder = {0};
119✔
337
  SDeleteRes req = {0};
119✔
338
  void* pTask = sStreamReaderInfo->pTask;
119✔
339

340
  req.uidList = taosArrayInit(0, sizeof(tb_uid_t));
119✔
341
  tDecoderInit(&decoder, data, len);
119✔
342
  STREAM_CHECK_RET_GOTO(tDecodeDeleteRes(&decoder, &req));
119!
343
  STREAM_CHECK_CONDITION_GOTO((sStreamReaderInfo->tableType == TSDB_SUPER_TABLE && !sStreamReaderInfo->isVtableStream && req.suid != sStreamReaderInfo->suid), TDB_CODE_SUCCESS);
119✔
344
  
345
  for (int32_t i = 0; i < taosArrayGetSize(req.uidList); i++) {
181✔
346
    uint64_t* uid = taosArrayGet(req.uidList, i);
104✔
347
    STREAM_CHECK_NULL_GOTO(uid, terrno);
131!
348
    uint64_t   id = 0;
104✔
349
    ST_TASK_ILOG("stream reader scan delete start data:uid %" PRIu64 ", skey %" PRIu64 ", ekey %" PRIu64, *uid, req.skey, req.ekey);
104!
350
    STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, req.suid, *uid, &id, false), TDB_CODE_SUCCESS);
104✔
351
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->deleteBlock, ((SSDataBlock*)rsp->deleteBlock)->info.rows + 1));
77!
352
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->deleteBlock, id, req.skey, req.ekey, ver));
77!
353
    ((SSDataBlock*)rsp->deleteBlock)->info.rows++;
77✔
354
    rsp->totalRows++;
77✔
355
  }
356

357
end:
77✔
358
  taosArrayDestroy(req.uidList);
119✔
359
  tDecoderClear(&decoder);
119✔
360
  return code;
119✔
361
}
362

363
static int32_t scanDropTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
1✔
364
                             int64_t ver) {
365
  int32_t  code = 0;
1✔
366
  int32_t  lino = 0;
1✔
367
  SDecoder decoder = {0};
1✔
368
  void* pTask = sStreamReaderInfo->pTask;
1✔
369

370
  SVDropTbBatchReq req = {0};
1✔
371
  tDecoderInit(&decoder, data, len);
1✔
372
  STREAM_CHECK_RET_GOTO(tDecodeSVDropTbBatchReq(&decoder, &req));
1!
373

374
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2✔
375
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
1✔
376
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
1!
377
    uint64_t id = 0;
1✔
378
    if(!uidInTableList(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id, false)) {
1!
379
      continue;
×
380
    }
381

382
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dropBlock, ((SSDataBlock*)rsp->dropBlock)->info.rows + 1));
1!
383
    STREAM_CHECK_RET_GOTO(buildDropTableBlock(rsp->dropBlock, id, ver));
1!
384
    ((SSDataBlock*)rsp->dropBlock)->info.rows++;
1✔
385
    rsp->totalRows++;
1✔
386
    ST_TASK_ILOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
1!
387
  }
388

389
end:
1✔
390
  tDecoderClear(&decoder);
1✔
391
  return code;
1✔
392
}
393

394
static int32_t reloadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo){
5,939✔
395
  (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
5,939✔
396
  qStreamDestroyTableList(sStreamReaderInfo->tableList);
5,939✔
397
  sStreamReaderInfo->tableList = NULL;
5,938✔
398
  int32_t code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, false);
5,938✔
399
  if (code == 0){
5,939!
400
    qStreamDestroyTableList(sStreamReaderInfo->historyTableList);
5,939✔
401
    sStreamReaderInfo->historyTableList = NULL;
5,939✔
402
    code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, true);
5,939✔
403
  }
404
  (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
5,939✔
405
  return code;
5,939✔
406
}
407

408
static int32_t scanCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
100✔
409
  int32_t  code = 0;
100✔
410
  int32_t  lino = 0;
100✔
411
  SDecoder decoder = {0};
100✔
412
  void* pTask = sStreamReaderInfo->pTask;
100✔
413

414
  SVCreateTbBatchReq req = {0};
100✔
415
  tDecoderInit(&decoder, data, len);
100✔
416
  
417
  STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbBatchReq(&decoder, &req));
100!
418

419
  bool found = false;
99✔
420
  SVCreateTbReq* pCreateReq = NULL;
99✔
421
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
152✔
422
    pCreateReq = req.pReqs + iReq;
99✔
423
    if (!needRefreshTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
99✔
424
      ST_TASK_ILOG("stream reader scan create table jump, %s", pCreateReq->name);
53!
425
      continue;
53✔
426
    }
427
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
47!
428

429
    found = true;
47✔
430
    break;
47✔
431
  }
432
  STREAM_CHECK_CONDITION_GOTO(!found, TDB_CODE_SUCCESS);
100✔
433

434
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
47!
435
end:
47✔
436
  tDeleteSVCreateTbBatchReq(&req);
100✔
437
  tDecoderClear(&decoder);
100✔
438
  return code;
100✔
439
}
440

441
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq) {
7,285✔
442
  int32_t  code = 0;
7,285✔
443
  int32_t  lino = 0;
7,285✔
444
  void*    pTask = sStreamReaderInfo->pTask;
7,285✔
445
  if (!needRefreshTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
7,285✔
446
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
1,413✔
447
    goto end;
1,415✔
448
  }
449
  ST_TASK_ILOG("stream reader scan auto create table %s", pCreateReq->name);
5,871!
450

451
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
5,871!
452
end:
5,871✔
453
  return code;
7,286✔
454
}
455

456
static int32_t scanAlterTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
124✔
457
  int32_t  code = 0;
124✔
458
  int32_t  lino = 0;
124✔
459
  SDecoder decoder = {0};
124✔
460
  void* pTask = sStreamReaderInfo->pTask;
124✔
461

462
  SVAlterTbReq req = {0};
124✔
463
  tDecoderInit(&decoder, data, len);
124✔
464
  
465
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
124!
466
  STREAM_CHECK_CONDITION_GOTO(req.action != TSDB_ALTER_TABLE_UPDATE_TAG_VAL && req.action != TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL, TDB_CODE_SUCCESS);
124!
467

468
  ETableType tbType = 0;
34✔
469
  uint64_t suid = 0;
34✔
470
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, req.tbName, &tbType, &suid));
34!
471
  STREAM_CHECK_CONDITION_GOTO(tbType != TSDB_CHILD_TABLE, TDB_CODE_SUCCESS);
34!
472
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
34✔
473

474
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
21!
475
  ST_TASK_ILOG("stream reader scan alter table %s", req.tbName);
21!
476

477
end:
×
478
  taosArrayDestroy(req.pMultiTag);
124✔
479
  tDecoderClear(&decoder);
124✔
480
  return code;
124✔
481
}
482

483
// static int32_t scanAlterSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
484
//   int32_t  code = 0;
485
//   int32_t  lino = 0;
486
//   SDecoder decoder = {0};
487
//   SMAlterStbReq reqAlter = {0};
488
//   SVCreateStbReq req = {0};
489
//   tDecoderInit(&decoder, data, len);
490
//   void* pTask = sStreamReaderInfo->pTask;
491
  
492
//   STREAM_CHECK_RET_GOTO(tDecodeSVCreateStbReq(&decoder, &req));
493
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
494
//   if (req.alterOriData != 0) {
495
//     STREAM_CHECK_RET_GOTO(tDeserializeSMAlterStbReq(req.alterOriData, req.alterOriDataLen, &reqAlter));
496
//     STREAM_CHECK_CONDITION_GOTO(reqAlter.alterType != TSDB_ALTER_TABLE_DROP_TAG && reqAlter.alterType != TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TDB_CODE_SUCCESS);
497
//   }
498
  
499
//   STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
500

501
//   ST_TASK_ILOG("stream reader scan alter suid %" PRId64, req.suid);
502
// end:
503
//   tFreeSMAltertbReq(&reqAlter);
504
//   tDecoderClear(&decoder);
505
//   return code;
506
// }
507

508
static int32_t scanDropSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
×
509
  int32_t  code = 0;
×
510
  int32_t  lino = 0;
×
511
  SDecoder decoder = {0};
×
512
  void* pTask = sStreamReaderInfo->pTask;
×
513

514
  SVDropStbReq req = {0};
×
515
  tDecoderInit(&decoder, data, len);
×
516
  STREAM_CHECK_RET_GOTO(tDecodeSVDropStbReq(&decoder, &req));
×
517
  STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
×
518
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
×
519

520
  ST_TASK_ILOG("stream reader scan drop suid %" PRId64, req.suid);
×
521
end:
×
522
  tDecoderClear(&decoder);
×
523
  return code;
×
524
}
525

526
static int32_t scanSubmitTbDataForMeta(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash) {
46,179✔
527
  int32_t code = 0;
46,179✔
528
  int32_t lino = 0;
46,179✔
529
  WalMetaResult walMeta = {0};
46,179✔
530
  SSubmitTbData submitTbData = {0};
46,179✔
531
  
532
  if (tStartDecode(pCoder) < 0) {
46,179!
533
    code = TSDB_CODE_INVALID_MSG;
×
534
    TSDB_CHECK_CODE(code, lino, end);
×
535
  }
536

537
  uint8_t       version = 0;
46,202✔
538
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
46,182!
539
    code = TSDB_CODE_INVALID_MSG;
×
540
    TSDB_CHECK_CODE(code, lino, end);
×
541
  }
542
  version = (submitTbData.flags >> 8) & 0xff;
46,182✔
543
  submitTbData.flags = submitTbData.flags & 0xff;
46,182✔
544

545
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
546
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
46,182✔
547
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
6,100!
548
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
6,100!
549
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
6,100!
550
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq));
6,099!
551
  }
552

553
  // submit data
554
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
46,163!
555
    code = TSDB_CODE_INVALID_MSG;
×
556
    TSDB_CHECK_CODE(code, lino, end);
×
557
  }
558
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
46,152!
559
    code = TSDB_CODE_INVALID_MSG;
×
560
    TSDB_CHECK_CODE(code, lino, end);
×
561
  }
562

563
  if (!uidInTableList(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &walMeta.id, false)){
46,152✔
564
    goto end;
34,038✔
565
  }
566
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
12,178!
567
    code = TSDB_CODE_INVALID_MSG;
×
568
    TSDB_CHECK_CODE(code, lino, end);
×
569
  }
570

571
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,178!
572
    uint64_t nColData = 0;
×
573
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
574
      code = TSDB_CODE_INVALID_MSG;
×
575
      TSDB_CHECK_CODE(code, lino, end);
×
576
    }
577

578
    SColData colData = {0};
×
579
    code = tDecodeColData(version, pCoder, &colData, false);
×
580
    if (code) {
×
581
      code = TSDB_CODE_INVALID_MSG;
×
582
      TSDB_CHECK_CODE(code, lino, end);
×
583
    }
584

585
    if (colData.flag != HAS_VALUE) {
×
586
      code = TSDB_CODE_INVALID_MSG;
×
587
      TSDB_CHECK_CODE(code, lino, end);
×
588
    }
589
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
590
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
591

592
    for (uint64_t i = 1; i < nColData; i++) {
×
593
      code = tDecodeColData(version, pCoder, &colData, true);
×
594
      if (code) {
×
595
        code = TSDB_CODE_INVALID_MSG;
×
596
        TSDB_CHECK_CODE(code, lino, end);
×
597
      }
598
    }
599
  } else {
600
    uint64_t nRow = 0;
12,178✔
601
    if (tDecodeU64v(pCoder, &nRow) < 0) {
12,172!
602
      code = TSDB_CODE_INVALID_MSG;
×
603
      TSDB_CHECK_CODE(code, lino, end);
×
604
    }
605

606
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
29,504✔
607
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
17,332✔
608
      pCoder->pos += pRow->len;
17,332✔
609
      if (iRow == 0){
17,332✔
610
#ifndef NO_UNALIGNED_ACCESS
611
        walMeta.skey = pRow->ts;
12,175✔
612
#else
613
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
614
#endif
615
      }
616
      if (iRow == nRow - 1) {
17,332✔
617
#ifndef NO_UNALIGNED_ACCESS
618
        walMeta.ekey = pRow->ts;
12,177✔
619
#else
620
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
621
#endif
622
      }
623
    }
624
  }
625

626
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
12,172✔
627
  if (data != NULL) {
12,174!
628
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
×
629
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
×
630
  } else {
631
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
12,174!
632
  }
633

634
end:
12,171✔
635
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
46,209✔
636
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
46,186!
637
  tEndDecode(pCoder);
46,186✔
638
  return code;
46,175✔
639
}
640

641
static int32_t scanSubmitDataForMeta(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
46,201✔
642
  int32_t  code = 0;
46,201✔
643
  int32_t  lino = 0;
46,201✔
644
  SDecoder decoder = {0};
46,201✔
645
  SSHashObj* gidHash = NULL;
46,201✔
646
  void* pTask = sStreamReaderInfo->pTask;
46,201✔
647

648
  tDecoderInit(&decoder, data, len);
46,201✔
649
  if (tStartDecode(&decoder) < 0) {
46,188!
650
    code = TSDB_CODE_INVALID_MSG;
×
651
    TSDB_CHECK_CODE(code, lino, end);
×
652
  }
653

654
  uint64_t nSubmitTbData = 0;
46,205✔
655
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
46,204!
656
    code = TSDB_CODE_INVALID_MSG;
×
657
    TSDB_CHECK_CODE(code, lino, end);
×
658
  }
659

660
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
46,204✔
661
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
46,193!
662

663
  for (int32_t i = 0; i < nSubmitTbData; i++) {
92,364✔
664
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash));
46,193!
665
  }
666
  tEndDecode(&decoder);
46,171✔
667

668
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
46,155!
669
  int32_t iter = 0;
46,141✔
670
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
46,141✔
671
  while (px != NULL) {
58,332✔
672
    WalMetaResult* pMeta = (WalMetaResult*)px;
12,170✔
673
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
12,170!
674
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
12,158✔
675
    rsp->totalRows++;
12,158✔
676
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
12,158✔
677
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
678
    px = tSimpleHashIterate(gidHash, px, &iter);
12,158✔
679
  }
680
end:
46,162✔
681
  tDecoderClear(&decoder);
46,162✔
682
  tSimpleHashCleanup( gidHash);
46,214✔
683
  return code;
46,203✔
684
}
685

686
static int32_t createBlockForTsdbMeta(SSDataBlock** pBlock, bool isVTable) {
980✔
687
  int32_t code = 0;
980✔
688
  int32_t lino = 0;
980✔
689
  SArray* schemas = taosArrayInit(8, sizeof(SSchema));
980✔
690
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
980!
691

692
  int32_t index = 1;
980✔
693
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // skey
980!
694
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // ekey
980!
695
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // uid
980!
696
  if (!isVTable) {
980✔
697
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_UBIGINT, LONG_BYTES, index++))  // gid
161!
698
  }
699
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))     // nrows
980!
700

701
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
980!
702

703
end:
980✔
704
  taosArrayDestroy(schemas);
980✔
705
  return code;
980✔
706
}
707

708
static int32_t createBlockForWalMetaNew(SSDataBlock** pBlock) {
651✔
709
  int32_t code = 0;
651✔
710
  int32_t lino = 0;
651✔
711
  SArray* schemas = NULL;
651✔
712

713
  schemas = taosArrayInit(8, sizeof(SSchema));
651✔
714
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
655!
715

716
  int32_t index = 0;
655✔
717
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
655!
718
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // skey
655!
719
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ekey
655!
720
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
655!
721

722
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
655!
723

724
end:
655✔
725
  taosArrayDestroy(schemas);
655✔
726
  return code;
655✔
727
}
728

729
static int32_t createBlockForDropTable(SSDataBlock** pBlock) {
1✔
730
  int32_t code = 0;
1✔
731
  int32_t lino = 0;
1✔
732
  SArray* schemas = NULL;
1✔
733

734
  schemas = taosArrayInit(8, sizeof(SSchema));
1✔
735
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
1!
736

737
  int32_t index = 0;
1✔
738
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
1!
739
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
1!
740

741
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
1!
742

743
end:
1✔
744
  taosArrayDestroy(schemas);
1✔
745
  return code;
1✔
746
}
747

748
static int32_t processMeta(int16_t msgType, SStreamTriggerReaderInfo* sStreamReaderInfo, void *data, int32_t len, SSTriggerWalNewRsp* rsp, int32_t ver) {
1,010✔
749
  int32_t code = 0;
1,010✔
750
  int32_t lino = 0;
1,010✔
751
  SDecoder dcoder = {0};
1,010✔
752
  tDecoderInit(&dcoder, data, len);
1,010✔
753
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
1,010✔
754
    if (rsp->deleteBlock == NULL) {
119✔
755
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock));
47!
756
    }
757
      
758
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
119!
759
  } else if (msgType == TDMT_VND_DROP_TABLE && sStreamReaderInfo->deleteOutTbl != 0) {
891✔
760
    if (rsp->dropBlock == NULL) {
1!
761
      STREAM_CHECK_RET_GOTO(createBlockForDropTable((SSDataBlock**)&rsp->dropBlock));
1!
762
    }
763
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
1!
764
  } else if (msgType == TDMT_VND_DROP_STB) {
890!
765
    STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
×
766
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
890✔
767
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, data, len));
100!
768
  } else if (msgType == TDMT_VND_ALTER_STB) {
790✔
769
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
770
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
632✔
771
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, data, len));
124!
772
  }
773

774
  end:
632✔
775
  tDecoderClear(&dcoder);
1,010✔
776
  return code;
1,010✔
777
}
778
static int32_t processWalVerMetaNew(SVnode* pVnode, SSTriggerWalNewRsp* rsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
13,729✔
779
                       int64_t ctime) {
780
  int32_t code = 0;
13,729✔
781
  int32_t lino = 0;
13,729✔
782
  void* pTask = sStreamReaderInfo->pTask;
13,729✔
783

784
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
13,729✔
785
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
13,739!
786
  code = walReaderSeekVer(pWalReader, rsp->ver);
13,739✔
787
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
13,735✔
788
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
10,603!
789
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
790
    }
791
    ST_TASK_DLOG("vgId:%d %s scan wal error:%s", TD_VID(pVnode), __func__, tstrerror(code));
10,602✔
792
    code = TSDB_CODE_SUCCESS;
10,602✔
793
    goto end;
10,602✔
794
  }
795
  STREAM_CHECK_RET_GOTO(code);
3,132!
796

797
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
3,132!
798
  while (1) {
46,828✔
799
    code = walNextValidMsg(pWalReader, true);
49,960✔
800
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){\
49,965✔
801
      ST_TASK_DLOG("vgId:%d %s scan wal error:%s", TD_VID(pVnode), __func__, tstrerror(code));
3,128✔
802
      code = TSDB_CODE_SUCCESS;
3,133✔
803
      goto end;
3,133✔
804
    }
805
    STREAM_CHECK_RET_GOTO(code);
46,837!
806
    rsp->ver = pWalReader->curVersion;
46,837✔
807
    SWalCont* wCont = &pWalReader->pHead->head;
46,837✔
808
    rsp->verTime = wCont->ingestTs;
46,837✔
809
    if (wCont->ingestTs / 1000 > ctime) break;
46,837!
810
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
46,837✔
811
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
46,837✔
812
    int64_t ver = wCont->version;
46,837✔
813

814
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 ", type:%d, deleteData:%d, deleteTb:%d",
46,837✔
815
      TD_VID(pVnode), ver, wCont->msgType, sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
816
    if (wCont->msgType == TDMT_VND_SUBMIT) {
46,837✔
817
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
46,207✔
818
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
46,207✔
819
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver));
46,207!
820
    } else {
821
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
630!
822
    }
823

824
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
46,828!
825
      break;
×
826
    }
827
  }
828

829
end:
13,735✔
830
  walCloseReader(pWalReader);
13,735✔
831
  return code;
13,742✔
832
}
833

834
static int32_t processTag(SVnode* pVnode, SStreamTriggerReaderInfo* info, bool isCalc, SStorageAPI* api, 
7,830✔
835
  uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks) {
836
  int32_t     code = 0;
7,830✔
837
  int32_t     lino = 0;
7,830✔
838
  SMetaReader mr = {0};
7,830✔
839
  SArray* tagCache = NULL;
7,830✔
840

841
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
7,830!
842
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
7,830!
843
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
7,830!
844
  if (numOfExpr == 0) {
7,830!
845
    return TSDB_CODE_SUCCESS;
×
846
  }
847

848
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
7,830✔
849
  if (uidData == NULL) {
7,854✔
850
    api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn);
320✔
851
    code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
320✔
852
    api->metaReaderFn.readerReleaseLock(&mr);
320✔
853
    STREAM_CHECK_RET_GOTO(code);
320!
854

855
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
320✔
856
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
320!
857
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
320✔
858
      taosArrayDestroyP(tagCache, taosMemFree);
1✔
859
      code = terrno;
×
860
      goto end;
×
861
    }
862
  } else {
863
    tagCache = *(SArray**)uidData;
7,534✔
864
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
7,534!
865
  }
866
  
867
  for (int32_t j = 0; j < numOfExpr; ++j) {
21,472✔
868
    const SExprInfo* pExpr1 = &pExprInfo[j];
13,674✔
869
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
13,674✔
870

871
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
13,674✔
872
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
13,632!
873
    int32_t functionId = pExpr1->pExpr->_function.functionId;
13,632✔
874

875
    // this is to handle the tbname
876
    if (fmIsScanPseudoColumnFunc(functionId)) {
13,632✔
877
      int32_t fType = pExpr1->pExpr->_function.functionType;
7,781✔
878
      if (fType == FUNCTION_TYPE_TBNAME) {
7,781!
879
        char   buf[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
7,846✔
880
        if (uidData == NULL) {
7,846✔
881
          STR_TO_VARSTR(buf, mr.me.name)
320✔
882
          char* tbname = taosStrdup(mr.me.name);
320!
883
          STREAM_CHECK_NULL_GOTO(tbname, terrno);
320!
884
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &tbname), terrno);
640!
885
        } else {
886
          char* tbname = taosArrayGetP(tagCache, j);
7,526✔
887
          STR_TO_VARSTR(buf, tbname)
7,536✔
888
        }
889
        for (uint32_t i = 0; i < numOfRows; i++){
22,723✔
890
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
14,867✔
891
        }
892
        code = colDataSetNItems(pColInfoData, currentRow, buf, numOfRows, numOfBlocks, false);
7,856✔
893
        pColInfoData->info.colId = -1;
7,830✔
894
      }
895
    } else {  // these are tags
896
      char* data = NULL;
5,831✔
897
      const char* p = NULL;
5,831✔
898
      STagVal tagVal = {0};
5,831✔
899
      if (uidData == NULL) {
5,831✔
900
        tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
261✔
901
        p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, pColInfoData->info.type, &tagVal);
261✔
902

903
        if (pColInfoData->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
261!
904
          data = tTagValToData((const STagVal*)p, false);
261✔
905
        } else {
906
          data = (char*)p;
×
907
        }
908

909
        if (data == NULL) {
261!
910
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
×
911
        } else {
912
          int32_t len = pColInfoData->info.bytes;
261✔
913
          if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
261!
914
            len = calcStrBytesByType(pColInfoData->info.type, (char*)data);
83✔
915
          }
916
          char* pData = taosMemoryCalloc(1, len);
261!
917
          STREAM_CHECK_NULL_GOTO(pData, terrno);
261!
918
          (void)memcpy(pData, data, len);
261✔
919
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &pData), terrno);
522!
920
        }
921
      } else {
922
        data = taosArrayGetP(tagCache, j);
5,570✔
923
      }
924

925
      bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
5,824!
926
      if (isNullVal) {
5,824!
927
        colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
928
      } else {
929
        for (uint32_t i = 0; i < numOfRows; i++){
38,375✔
930
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
32,551✔
931
        }
932
        code = colDataSetNItems(pColInfoData, currentRow, data, numOfRows, numOfBlocks, false);
5,824✔
933
        if (uidData == NULL && pColInfoData->info.type != TSDB_DATA_TYPE_JSON && IS_VAR_DATA_TYPE(((const STagVal*)p)->type)) {
5,859!
934
          taosMemoryFree(data);
83!
935
        }
936
        STREAM_CHECK_RET_GOTO(code);
5,856!
937
      }
938
    }
939
  }
940

941
end:
7,798✔
942
  api->metaReaderFn.clearReader(&mr);
7,798✔
943
  return code;
7,811✔
944
}
945

946
int32_t getRowRange(SColData* pCol, STimeWindow* window, int32_t* rowStart, int32_t* rowEnd, int32_t* nRows) {
×
947
  int32_t code = 0;
×
948
  int32_t lino = 0;
×
949
  *nRows = 0;
×
950
  *rowStart = 0;
×
951
  *rowEnd = pCol->nVal;
×
952
  if (window != NULL) {
×
953
    SColVal colVal = {0};
×
954
    *rowStart = -1;
×
955
    *rowEnd = -1;
×
956
    for (int32_t k = 0; k < pCol->nVal; k++) {
×
957
      STREAM_CHECK_RET_GOTO(tColDataGetValue(pCol, k, &colVal));
×
958
      int64_t ts = VALUE_GET_TRIVIAL_DATUM(&colVal.value);
×
959
      if (ts >= window->skey && *rowStart == -1) {
×
960
        *rowStart = k;
×
961
      }
962
      if (ts > window->ekey && *rowEnd == -1) {
×
963
        *rowEnd = k;
×
964
      }
965
    }
966
    STREAM_CHECK_CONDITION_GOTO(*rowStart == -1 || *rowStart == *rowEnd, TDB_CODE_SUCCESS);
×
967

968
    if (*rowStart != -1 && *rowEnd == -1) {
×
969
      *rowEnd = pCol->nVal;
×
970
    }
971
  }
972
  *nRows = *rowEnd - *rowStart;
×
973

974
end:
×
975
  return code;
×
976
}
977

978
static int32_t setColData(int64_t rows, int32_t rowStart, int32_t rowEnd, SColData* colData, SColumnInfoData* pColData) {
×
979
  int32_t code = 0;
×
980
  int32_t lino = 0;
×
981
  for (int32_t k = rowStart; k < rowEnd; k++) {
×
982
    SColVal colVal = {0};
×
983
    STREAM_CHECK_RET_GOTO(tColDataGetValue(colData, k, &colVal));
×
984
    STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, rows + k, VALUE_GET_DATUM(&colVal.value, colVal.value.type),
×
985
                                        !COL_VAL_IS_VALUE(&colVal)));
986
  }
987
  end:
×
988
  return code;
×
989
}
990

991
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, 
22,041✔
992
  STSchema** schemas, SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
993
  int32_t code = 0;
22,041✔
994
  int32_t lino = 0;
22,041✔
995
  uint64_t id = 0;
22,041✔
996
  WalMetaResult walMeta = {0};
22,041✔
997
  void* pTask = sStreamReaderInfo->pTask;
22,041✔
998
  SSDataBlock * pBlock = (SSDataBlock*)rsp->dataBlock;
22,041✔
999

1000
  if (tStartDecode(pCoder) < 0) {
22,041!
1001
    code = TSDB_CODE_INVALID_MSG;
×
1002
    TSDB_CHECK_CODE(code, lino, end);
×
1003
  }
1004

1005
  SSubmitTbData submitTbData = {0};
22,074✔
1006
  uint8_t       version = 0;
22,074✔
1007
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
22,053!
1008
    code = TSDB_CODE_INVALID_MSG;
×
1009
    TSDB_CHECK_CODE(code, lino, end);
×
1010
  }
1011
  version = (submitTbData.flags >> 8) & 0xff;
22,053✔
1012
  submitTbData.flags = submitTbData.flags & 0xff;
22,053✔
1013
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1014
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
22,053✔
1015
    if (tStartDecode(pCoder) < 0) {
98!
1016
      code = TSDB_CODE_INVALID_MSG;
×
1017
      TSDB_CHECK_CODE(code, lino, end);
×
1018
    }
1019
    tEndDecode(pCoder);
98✔
1020
  }
1021

1022
  // submit data
1023
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
22,018!
1024
    code = TSDB_CODE_INVALID_MSG;
×
1025
    TSDB_CHECK_CODE(code, lino, end);
×
1026
  }
1027
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
21,994!
1028
    code = TSDB_CODE_INVALID_MSG;
×
1029
    TSDB_CHECK_CODE(code, lino, end);
×
1030
  }
1031

1032
  STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &id, rsp->isCalc), TDB_CODE_SUCCESS);
21,994✔
1033

1034
  walMeta.id = id;
19,058✔
1035
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
19,058✔
1036

1037
  if (ranges != NULL){
19,058✔
1038
    void* timerange = tSimpleHashGet(ranges, &id, sizeof(id));
15,044✔
1039
    if (timerange == NULL) goto end;;
15,038!
1040
    int64_t* pRange = (int64_t*)timerange;
15,038✔
1041
    window.skey = pRange[0];
15,038✔
1042
    window.ekey = pRange[1];
15,038✔
1043
  }
1044
  
1045
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
19,090!
1046
    code = TSDB_CODE_INVALID_MSG;
×
1047
    TSDB_CHECK_CODE(code, lino, end);
×
1048
  }
1049

1050
  if (*schemas == NULL) {
19,090!
1051
    *schemas = metaGetTbTSchema(pVnode->pMeta, submitTbData.suid != 0 ? submitTbData.suid : submitTbData.uid, submitTbData.sver, 1);
19,090✔
1052
    STREAM_CHECK_NULL_GOTO(*schemas, TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND);
19,104!
1053
  }
1054

1055
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(sStreamReaderInfo->indexHash, &submitTbData.uid, LONG_BYTES);
19,104✔
1056
  STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
19,099!
1057
  int32_t blockStart = pSlice->currentRowIdx;
19,099✔
1058

1059
  int32_t numOfRows = 0;
19,099✔
1060
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
19,099!
1061
    uint64_t nColData = 0;
×
1062
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1063
      code = TSDB_CODE_INVALID_MSG;
×
1064
      TSDB_CHECK_CODE(code, lino, end);
×
1065
    }
1066

1067
    SColData colData = {0};
×
1068
    code = tDecodeColData(version, pCoder, &colData, false);
×
1069
    if (code) {
×
1070
      code = TSDB_CODE_INVALID_MSG;
×
1071
      TSDB_CHECK_CODE(code, lino, end);
×
1072
    }
1073

1074
    if (colData.flag != HAS_VALUE) {
×
1075
      code = TSDB_CODE_INVALID_MSG;
×
1076
      TSDB_CHECK_CODE(code, lino, end);
×
1077
    }
1078
    
1079
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
1080
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
1081

1082
    int32_t rowStart = 0;
×
1083
    int32_t rowEnd = 0;
×
1084
    STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, &numOfRows));
×
1085
    STREAM_CHECK_CONDITION_GOTO(numOfRows <= 0, TDB_CODE_SUCCESS);
×
1086

1087
    int32_t pos = pCoder->pos;
×
1088
    for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
×
1089
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
×
1090
      STREAM_CHECK_NULL_GOTO(pColData, terrno);
×
1091
      if (pColData->info.colId <= -1) {
×
1092
        pColData->hasNull = true;
×
1093
        continue;
×
1094
      }
1095
      if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
×
1096
        STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colData, pColData));
×
1097
        continue;
×
1098
      }
1099

1100
      pCoder->pos = pos;
×
1101

1102
      int16_t colId = 0;
×
1103
      if (sStreamReaderInfo->isVtableStream){
×
1104
        int64_t id[2] = {submitTbData.suid, submitTbData.uid};
×
1105
        void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
×
1106
        STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
×
1107
        SSHashObj* uInfo = *(SSHashObj **)px;
×
1108
        STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
×
1109
        int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
×
1110
        if (tmp != NULL) {
×
1111
          colId = *tmp;
×
1112
        } else {
1113
          colId = -1;
×
1114
        }
1115
      } else {
1116
        colId = pColData->info.colId;
×
1117
      }
1118
      
1119
      uint64_t j = 1;
×
1120
      for (; j < nColData; j++) {
×
1121
        int16_t cid = 0;
×
1122
        int32_t posTmp = pCoder->pos;
×
1123
        pCoder->pos += INT_BYTES;
×
1124
        if ((code = tDecodeI16v(pCoder, &cid))) return code;
×
1125
        pCoder->pos = posTmp;
×
1126
        if (cid == colId) {
×
1127
          SColData colDataTmp = {0};
×
1128
          code = tDecodeColData(version, pCoder, &colDataTmp, false);
×
1129
          if (code) {
×
1130
            code = TSDB_CODE_INVALID_MSG;
×
1131
            TSDB_CHECK_CODE(code, lino, end);
×
1132
          }
1133
          STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colDataTmp, pColData));
×
1134
          break;
×
1135
        }
1136
        code = tDecodeColData(version, pCoder, &colData, true);
×
1137
        if (code) {
×
1138
          code = TSDB_CODE_INVALID_MSG;
×
1139
          TSDB_CHECK_CODE(code, lino, end);
×
1140
        }
1141
      }
1142
      if (j == nColData) {
×
1143
        colDataSetNNULL(pColData, blockStart, numOfRows);
×
1144
      }
1145
    }
1146
  } else {
1147
    uint64_t nRow = 0;
19,099✔
1148
    if (tDecodeU64v(pCoder, &nRow) < 0) {
19,092!
1149
      code = TSDB_CODE_INVALID_MSG;
×
1150
      TSDB_CHECK_CODE(code, lino, end);
×
1151
    }
1152
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
43,230✔
1153
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
24,190✔
1154
      pCoder->pos += pRow->len;
24,190✔
1155

1156
      if (iRow == 0){
24,190✔
1157
#ifndef NO_UNALIGNED_ACCESS
1158
        walMeta.skey = pRow->ts;
19,095✔
1159
#else
1160
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1161
#endif
1162
      }
1163
      if (iRow == nRow - 1) {
24,190✔
1164
#ifndef NO_UNALIGNED_ACCESS
1165
        walMeta.ekey = pRow->ts;
19,095✔
1166
#else
1167
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
1168
#endif
1169
      }
1170

1171
      if (pRow->ts < window.skey || pRow->ts > window.ekey) {
24,190!
1172
        continue;
36✔
1173
      }
1174
     
1175
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {  // reader todo test null
160,511✔
1176
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
135,959✔
1177
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
136,076!
1178
        if (pColData->info.colId <= -1) {
136,422✔
1179
          pColData->hasNull = true;
43,719✔
1180
          continue;
43,719✔
1181
        }
1182
        int16_t colId = 0;
92,703✔
1183
        if (sStreamReaderInfo->isVtableStream){
92,703✔
1184
          int64_t id[2] = {submitTbData.suid, submitTbData.uid};
34,155✔
1185
          void* px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
34,155✔
1186
          STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
34,172!
1187
          SSHashObj* uInfo = *(SSHashObj**)px;
34,172✔
1188
          STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
34,172!
1189
          int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
34,172✔
1190
          if (tmp != NULL) {
34,155✔
1191
            colId = *tmp;
31,100✔
1192
          } else {
1193
            colId = -1;
3,055✔
1194
          }
1195
          ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
34,155!
1196
        } else {
1197
          colId = pColData->info.colId;
58,548✔
1198
        }
1199
        
1200
        SColVal colVal = {0};
92,705✔
1201
        int32_t sourceIdx = 0;
92,705✔
1202
        while (1) {
1203
          if (sourceIdx >= (*schemas)->numOfCols) {
253,602✔
1204
            break;
33,750✔
1205
          }
1206
          STREAM_CHECK_RET_GOTO(tRowGet(pRow, *schemas, sourceIdx, &colVal));
219,852!
1207
          if (colVal.cid == colId) {
219,749✔
1208
            break;
58,852✔
1209
          }
1210
          sourceIdx++;
160,897✔
1211
        }
1212
        if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
92,602✔
1213
          if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL){
55,579!
1214
            STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart+ numOfRows, (const char*)colVal.value.pData, colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
68!
1215
          } else {
1216
            STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)), !COL_VAL_IS_VALUE(&colVal)));
55,511!
1217
          }
1218
        } else {
1219
          colDataSetNULL(pColData, blockStart + numOfRows);
37,023✔
1220
        }
1221
      }
1222
      
1223
      numOfRows++;
24,102✔
1224
    }
1225
  }
1226

1227
  if (numOfRows > 0) {
19,040!
1228
    if (!sStreamReaderInfo->isVtableStream) {
19,040✔
1229
      SStorageAPI  api = {0};
7,617✔
1230
      initStorageAPI(&api);
7,617✔
1231
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, rsp->isCalc, &api, submitTbData.uid, pBlock, blockStart, numOfRows, 1));
7,633!
1232
    }
1233
    
1234
    SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
19,039✔
1235
    STREAM_CHECK_NULL_GOTO(pColData, terrno);
19,035!
1236
    STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
19,035!
1237
  }
1238

1239
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
19,066✔
1240
    ", uid:%" PRId64 ", ver:%d, row index:%d, rows:%d", __func__, window.skey, window.ekey, 
1241
    id, submitTbData.uid, submitTbData.sver, pSlice->currentRowIdx, numOfRows);
1242
  pSlice->currentRowIdx += numOfRows;
19,065✔
1243
  pBlock->info.rows += numOfRows;
19,065✔
1244
  
1245
  if (gidHash == NULL) goto end;
19,065✔
1246

1247
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
4,022✔
1248
  if (data != NULL) {
4,016!
1249
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
×
1250
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
×
1251
  } else {
1252
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
4,016!
1253
  }
1254

1255
end:
4,009✔
1256
  if (code != 0) {                                                             \
22,042!
1257
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code)); \
×
1258
  }
1259
  tEndDecode(pCoder);
22,042✔
1260
  return code;
21,989✔
1261
}
1262
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
22,044✔
1263
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
1264
  int32_t  code = 0;
22,044✔
1265
  int32_t  lino = 0;
22,044✔
1266
  STSchema* schemas = NULL;
22,044✔
1267
  SDecoder decoder = {0};
22,044✔
1268
  SSHashObj* gidHash = NULL;
22,044✔
1269
  void* pTask = sStreamReaderInfo->pTask;
22,044✔
1270

1271
  tDecoderInit(&decoder, data, len);
22,044✔
1272
  if (tStartDecode(&decoder) < 0) {
22,023!
1273
    code = TSDB_CODE_INVALID_MSG;
×
1274
    TSDB_CHECK_CODE(code, lino, end);
×
1275
  }
1276

1277
  uint64_t nSubmitTbData = 0;
22,065✔
1278
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
22,044!
1279
    code = TSDB_CODE_INVALID_MSG;
×
1280
    TSDB_CHECK_CODE(code, lino, end);
×
1281
  }
1282

1283
  if (rsp->metaBlock != NULL){
22,044✔
1284
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,988✔
1285
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
7,009!
1286
  }
1287

1288
  for (int32_t i = 0; i < nSubmitTbData; i++) {
44,043✔
1289
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, &schemas, ranges, gidHash, rsp, ver));
22,068!
1290
  }
1291

1292
  tEndDecode(&decoder);
21,975✔
1293

1294
  if (rsp->metaBlock != NULL){
21,952✔
1295
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
6,933!
1296
    int32_t iter = 0;
6,933✔
1297
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
6,933✔
1298
    while (px != NULL) {
11,003✔
1299
      WalMetaResult* pMeta = (WalMetaResult*)px;
4,034✔
1300
      STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
4,034!
1301
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
3,996✔
1302
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
3,996✔
1303
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1304
      px = tSimpleHashIterate(gidHash, px, &iter);
3,996✔
1305
    }
1306
  }
1307
  
1308

1309
end:
15,019✔
1310
  taosMemoryFree(schemas);
21,988!
1311
  tSimpleHashCleanup(gidHash);
22,044✔
1312
  tDecoderClear(&decoder);
22,018✔
1313
  return code;
22,108✔
1314
}
1315

1316
static int32_t scanSubmitTbDataPre(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, 
24,082✔
1317
  uint64_t* gid, int64_t* uid, int32_t* numOfRows, bool isCalc) {
1318
  int32_t code = 0;
24,082✔
1319
  int32_t lino = 0;
24,082✔
1320
  void* pTask = sStreamReaderInfo->pTask;
24,082✔
1321

1322
  if (tStartDecode(pCoder) < 0) {
24,082!
1323
    code = TSDB_CODE_INVALID_MSG;
×
1324
    TSDB_CHECK_CODE(code, lino, end);
×
1325
  }
1326

1327
  SSubmitTbData submitTbData = {0};
24,087✔
1328
  uint8_t       version = 0;
24,087✔
1329
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
24,075!
1330
    code = TSDB_CODE_INVALID_MSG;
×
1331
    TSDB_CHECK_CODE(code, lino, end);
×
1332
  }
1333
  version = (submitTbData.flags >> 8) & 0xff;
24,075✔
1334
  submitTbData.flags = submitTbData.flags & 0xff;
24,075✔
1335

1336
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1337
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
24,075✔
1338
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,187!
1339
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
1,190!
1340
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
1,190!
1341
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq));
1,187!
1342
  }
1343

1344
  // submit data
1345
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
23,993!
1346
    code = TSDB_CODE_INVALID_MSG;
×
1347
    TSDB_CHECK_CODE(code, lino, end);
×
1348
  }
1349
  if (tDecodeI64(pCoder, uid) < 0) {
23,911!
1350
    code = TSDB_CODE_INVALID_MSG;
×
1351
    TSDB_CHECK_CODE(code, lino, end);
×
1352
  }
1353

1354
  STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, submitTbData.suid, *uid, gid, isCalc), TDB_CODE_SUCCESS);
23,911✔
1355

1356
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
18,950✔
1357

1358
  if (ranges != NULL){
18,950✔
1359
    void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
15,048✔
1360
    if (timerange == NULL) goto end;;
15,037!
1361
    int64_t* pRange = (int64_t*)timerange;
15,037✔
1362
    window.skey = pRange[0];
15,037✔
1363
    window.ekey = pRange[1];
15,037✔
1364
  }
1365
  
1366
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
19,015!
1367
    code = TSDB_CODE_INVALID_MSG;
×
1368
    TSDB_CHECK_CODE(code, lino, end);
×
1369
  }
1370

1371
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
19,015!
1372
    uint64_t nColData = 0;
×
1373
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1374
      code = TSDB_CODE_INVALID_MSG;
×
1375
      TSDB_CHECK_CODE(code, lino, end);
×
1376
    }
1377

1378
    SColData colData = {0};
×
1379
    code = tDecodeColData(version, pCoder, &colData, false);
×
1380
    if (code) {
×
1381
      code = TSDB_CODE_INVALID_MSG;
×
1382
      TSDB_CHECK_CODE(code, lino, end);
×
1383
    }
1384

1385
    if (colData.flag != HAS_VALUE) {
×
1386
      code = TSDB_CODE_INVALID_MSG;
×
1387
      TSDB_CHECK_CODE(code, lino, end);
×
1388
    }
1389
    int32_t rowStart = 0;
×
1390
    int32_t rowEnd = 0;
×
1391
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
×
1392
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, numOfRows));
×
1393
    } else {
1394
      (*numOfRows) = colData.nVal;
×
1395
    } 
1396
  } else {
1397
    uint64_t nRow = 0;
19,015✔
1398
    if (tDecodeU64v(pCoder, &nRow) < 0) {
19,002!
1399
      code = TSDB_CODE_INVALID_MSG;
×
1400
      TSDB_CHECK_CODE(code, lino, end);
×
1401
    }
1402

1403
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) { 
19,002!
1404
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
35,099✔
1405
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
20,075✔
1406
        pCoder->pos += pRow->len;
20,075✔
1407
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
20,075!
1408
          continue;
36✔
1409
        }
1410
        (*numOfRows)++;
20,039✔
1411
      }
1412
    } else {
1413
      (*numOfRows) = nRow;
3,978✔
1414
    }
1415
  }
1416
  
1417
end:
24,195✔
1418
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
24,195✔
1419
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
24,049!
1420
  tEndDecode(pCoder);
24,051✔
1421
  return code;
24,022✔
1422
}
1423

1424
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
24,040✔
1425
  int32_t  code = 0;
24,040✔
1426
  int32_t  lino = 0;
24,040✔
1427
  SDecoder decoder = {0};
24,040✔
1428
  void* pTask = sStreamReaderInfo->pTask;
24,040✔
1429

1430
  tDecoderInit(&decoder, data, len);
24,040✔
1431
  if (tStartDecode(&decoder) < 0) {
24,015!
1432
    code = TSDB_CODE_INVALID_MSG;
×
1433
    TSDB_CHECK_CODE(code, lino, end);
×
1434
  }
1435

1436
  uint64_t nSubmitTbData = 0;
24,096✔
1437
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
24,094!
1438
    code = TSDB_CODE_INVALID_MSG;
×
1439
    TSDB_CHECK_CODE(code, lino, end);
×
1440
  }
1441

1442
  for (int32_t i = 0; i < nSubmitTbData; i++) {
48,261✔
1443
    uint64_t gid = -1;
24,064✔
1444
    int64_t  uid = 0;
24,064✔
1445
    int32_t numOfRows = 0;
24,064✔
1446
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp->isCalc));
24,064!
1447
    if (numOfRows <= 0) {
23,992✔
1448
      continue;
5,174✔
1449
    }
1450
    rsp->totalRows += numOfRows;
18,818✔
1451

1452
    SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(sStreamReaderInfo->indexHash, &uid, LONG_BYTES);
18,818✔
1453
    if (pSlice != NULL) {
18,973✔
1454
      pSlice->numRows += numOfRows;
18,415✔
1455
      ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d", __func__, uid, gid, pSlice->numRows);
18,415✔
1456
      pSlice->gId = gid;
18,418✔
1457
    } else {
1458
      SStreamWalDataSlice tmp = {.gId=gid,.numRows=numOfRows,.currentRowIdx=0,.startRowIdx=0};
558✔
1459
      ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d", __func__, uid, gid, tmp.numRows);
558✔
1460
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(sStreamReaderInfo->indexHash, &uid, LONG_BYTES, &tmp, sizeof(tmp)));
558!
1461
    } 
1462
  }
1463

1464
  tEndDecode(&decoder);
24,197✔
1465

1466
end:
23,982✔
1467
  tDecoderClear(&decoder);
23,982✔
1468
  return code;
24,234✔
1469
}
1470

1471
static void resetIndexHash(SSHashObj* indexHash){
16,049✔
1472
  void*   pe = NULL;
16,049✔
1473
  int32_t iter = 0;
16,049✔
1474
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
38,525✔
1475
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
22,476✔
1476
    pInfo->startRowIdx = 0;
22,476✔
1477
    pInfo->currentRowIdx = 0;
22,476✔
1478
    pInfo->numRows = 0;
22,476✔
1479
    pInfo->gId = -1;
22,476✔
1480
  }
1481
}
15,975✔
1482

1483
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
1,298✔
1484
  void*   pe = NULL;
1,298✔
1485
  int32_t iter = 0;
1,298✔
1486
  int32_t index = 0;
1,298✔
1487
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
4,958✔
1488
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
3,660✔
1489
    pInfo->startRowIdx = index;
3,660✔
1490
    pInfo->currentRowIdx = index;
3,660✔
1491
    index += pInfo->numRows;
3,660✔
1492
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
5,442!
1493
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1494
  }
1495
}
1,295✔
1496

1497
static void printIndexHash(SSHashObj* indexHash, void* pTask){
1,296✔
1498
  void*   pe = NULL;
1,296✔
1499
  int32_t iter = 0;
1,296✔
1500
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
4,954✔
1501
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
3,658✔
1502
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
5,440!
1503
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1504
  }
1505
}
1,296✔
1506

1507
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
26✔
1508
  void*   pe = NULL;
26✔
1509
  int32_t iter = 0;
26✔
1510
  int32_t index = 0;
26✔
1511
  int32_t pIndex = 0;
26✔
1512
  int8_t* pIndicator = (int8_t*)pRet->pData;
26✔
1513
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
80✔
1514
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
54✔
1515
    pInfo->startRowIdx = index;
54✔
1516
    int32_t size = pInfo->numRows;
54✔
1517
    for (int32_t i = 0; i < pInfo->numRows; i++) {
342✔
1518
      if (pIndicator && !pIndicator[pIndex++]) {
288!
1519
        size--;
106✔
1520
      }
1521
    }
1522
    pInfo->numRows = size;
54✔
1523
    index += pInfo->numRows;
54✔
1524
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
54!
1525
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1526
  }
1527
}
26✔
1528

1529
static int32_t prepareIndexMetaData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* resultRsp){
7,625✔
1530
  int32_t      code = 0;
7,625✔
1531
  int32_t      lino = 0;
7,625✔
1532
  void* pTask = sStreamReaderInfo->pTask;
7,625✔
1533

1534
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
7,625✔
1535
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
7,648✔
1536
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
6,707!
1537
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
1538
    }
1539
    ST_TASK_DLOG("%s scan wal error:%s",  __func__, tstrerror(code));
6,705✔
1540
    code = TSDB_CODE_SUCCESS;
6,703✔
1541
    goto end;
6,703✔
1542
  }
1543
  STREAM_CHECK_RET_GOTO(code);
941!
1544

1545
  while (1) {
9,532✔
1546
    code = walNextValidMsg(pWalReader, true);
10,473✔
1547
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
10,353✔
1548
      ST_TASK_DLOG("%s scan wal error:%s", __func__, tstrerror(code));
920✔
1549
      code = TSDB_CODE_SUCCESS;
936✔
1550
      goto end;
936✔
1551
    }
1552
    STREAM_CHECK_RET_GOTO(code);
9,433!
1553
    resultRsp->ver = pWalReader->curVersion;
9,433✔
1554
    SWalCont* wCont = &pWalReader->pHead->head;
9,433✔
1555
    resultRsp->verTime = wCont->ingestTs;
9,433✔
1556
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
9,433✔
1557
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
9,433✔
1558
    int64_t ver = wCont->version;
9,433✔
1559
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%d, deleteData:%d, deleteTb:%d", __func__,
9,433✔
1560
      ver, wCont->msgType, sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
1561
    if (wCont->msgType == TDMT_VND_SUBMIT) {
9,412✔
1562
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
9,026✔
1563
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
9,026✔
1564
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp));
9,026!
1565
    } else if (wCont->msgType == TDMT_VND_ALTER_TABLE && resultRsp->totalRows > 0) {
386✔
1566
      resultRsp->ver--;
5✔
1567
      break;
5✔
1568
    } else {
1569
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
381!
1570
    }
1571

1572
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
9,526✔
1573
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
9,532!
1574
      break;
×
1575
    }
1576
  }
1577
  
1578
end:
7,644✔
1579
  STREAM_PRINT_LOG_END(code, lino);
7,644!
1580
  return code;
7,640✔
1581
}
1582

1583
static int32_t prepareIndexData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, 
8,392✔
1584
  SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp){
1585
  int32_t      code = 0;
8,392✔
1586
  int32_t      lino = 0;
8,392✔
1587

1588
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
23,443✔
1589
    int64_t *ver = taosArrayGet(versions, i);
15,043✔
1590
    if (ver == NULL) continue;
15,046!
1591

1592
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
15,046!
1593
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
15,060!
1594
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
1595
      continue;
×
1596
    }
1597
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
15,060!
1598

1599
    SWalCont* wCont = &pWalReader->pHead->head;
15,053✔
1600
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
15,053✔
1601
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
15,053✔
1602

1603
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp));
15,053!
1604
  }
1605
  
1606
end:
8,394✔
1607
  return code;
8,394✔
1608
}
1609

1610
static int32_t filterData(SSTriggerWalNewRsp* resultRsp, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,298✔
1611
  int32_t      code = 0;
1,298✔
1612
  int32_t       lino = 0;
1,298✔
1613
  SColumnInfoData* pRet = NULL;
1,298✔
1614
  int64_t totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
1,298✔
1615
  STREAM_CHECK_RET_GOTO(qStreamFilter(((SSDataBlock*)resultRsp->dataBlock), sStreamReaderInfo->pFilterInfo, &pRet));
1,298!
1616
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
1,299✔
1617
    filterIndexHash(sStreamReaderInfo->indexHash, pRet);
26✔
1618
  }
1619

1620
end:
1,273✔
1621
  colDataDestroy(pRet);
1,299✔
1622
  taosMemoryFree(pRet);
1,298!
1623
  return code;
1,299✔
1624
}
1625

1626
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
7,661✔
1627
                                    SSTriggerWalNewRsp* resultRsp) {
1628
  int32_t      code = 0;
7,661✔
1629
  int32_t      lino = 0;
7,661✔
1630
  void* pTask = sStreamReaderInfo->pTask;
7,661✔
1631
                                        
1632
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
7,661✔
1633
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
7,655!
1634
  resetIndexHash(sStreamReaderInfo->indexHash);
7,655✔
1635
  blockDataEmpty(resultRsp->dataBlock);
7,649✔
1636
  blockDataEmpty(resultRsp->metaBlock);
7,601✔
1637
  int64_t lastVer = resultRsp->ver;                                      
7,623✔
1638
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp));
7,623!
1639
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
7,642✔
1640

1641
  buildIndexHash(sStreamReaderInfo->indexHash, pTask);
299✔
1642
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
298!
1643
  while(lastVer < resultRsp->ver) {
7,629✔
1644
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
7,328!
1645
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
7,315✔
1646
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
285!
1647
      continue;
286✔
1648
    }
1649
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
7,030!
1650
    SWalCont* wCont = &pWalReader->pHead->head;
7,002✔
1651
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
7,002✔
1652
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
7,002✔
1653

1654
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
7,002!
1655
  }
1656

1657
  int32_t metaRows = resultRsp->totalRows - ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
301✔
1658
  STREAM_CHECK_RET_GOTO(filterData(resultRsp, sStreamReaderInfo));
301!
1659
  resultRsp->totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows + metaRows;
300✔
1660

1661
end:
7,643✔
1662
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
7,643✔
1663
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
1664
  walCloseReader(pWalReader);
7,643✔
1665
  return code;
7,668✔
1666
}
1667

1668
static int32_t processWalVerDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
8,396✔
1669
                                    SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
1670
  int32_t      code = 0;
8,396✔
1671
  int32_t      lino = 0;
8,396✔
1672

1673
  void* pTask = sStreamReaderInfo->pTask;
8,396✔
1674
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
8,396✔
1675
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
8,394!
1676
  
1677
  if (taosArrayGetSize(versions) > 0) {
8,394✔
1678
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
998✔
1679
  }
1680
  
1681
  resetIndexHash(sStreamReaderInfo->indexHash);
8,394✔
1682
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
8,392!
1683
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
8,394✔
1684

1685
  buildIndexHash(sStreamReaderInfo->indexHash, pTask);
999✔
1686

1687
  blockDataEmpty(rsp->dataBlock);
999✔
1688
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dataBlock, rsp->totalRows));
999!
1689

1690
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
16,052✔
1691
    int64_t *ver = taosArrayGet(versions, i);
15,053✔
1692
    if (ver == NULL) continue;
15,049!
1693

1694
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
15,049!
1695
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
15,068!
1696
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
1697
      continue;
×
1698
    }
1699
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
15,068!
1700
    SWalCont* wCont = &pWalReader->pHead->head;
15,047✔
1701
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
15,047✔
1702
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
15,047✔
1703

1704
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
15,047!
1705
  }
1706
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
1707
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
995!
1708
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
999✔
1709

1710
end:
8,394✔
1711
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
8,394✔
1712
            rsp->totalRows, rsp->ver, walGetAppliedVer(pWalReader->pWal));
1713
  walCloseReader(pWalReader);
8,394✔
1714
  return code;
8,399✔
1715
}
1716

1717
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas) {
823✔
1718
  int32_t code = 0;
823✔
1719
  int32_t lino = 0;
823✔
1720
  SMetaReader metaReader = {0};
823✔
1721
  SStorageAPI api = {0};
823✔
1722
  initStorageAPI(&api);
823✔
1723
  *schemas = taosArrayInit(8, sizeof(SSchema));
823✔
1724
  STREAM_CHECK_NULL_GOTO(*schemas, terrno);
823!
1725
  
1726
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
823✔
1727
  STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, uid));
823✔
1728

1729
  SSchemaWrapper* sSchemaWrapper = NULL;
821✔
1730
  if (metaReader.me.type == TD_CHILD_TABLE) {
821!
1731
    int64_t suid = metaReader.me.ctbEntry.suid;
821✔
1732
    tDecoderClear(&metaReader.coder);
821✔
1733
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, suid));
821!
1734
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
821✔
1735
  } else if (metaReader.me.type == TD_NORMAL_TABLE) {
×
1736
    sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
×
1737
  } else {
1738
    qError("invalid table type:%d", metaReader.me.type);
×
1739
  }
1740

1741
  for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
4,808✔
1742
    SSchema* s = sSchemaWrapper->pSchema + j;
3,987✔
1743
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
7,974!
1744
  }
1745

1746
end:
821✔
1747
  api.metaReaderFn.clearReader(&metaReader);
823✔
1748
  STREAM_PRINT_LOG_END(code, lino);
823!
1749
  if (code != 0)  {
823✔
1750
    taosArrayDestroy(*schemas);
2✔
1751
    *schemas = NULL;
2✔
1752
  }
1753
  return code;
823✔
1754
}
1755

1756
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
821✔
1757
  int32_t code = 0;
821✔
1758
  int32_t lino = 0;
821✔
1759
  size_t  schemaLen = taosArrayGetSize(schemas);
821✔
1760
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
821!
1761
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
3,131✔
1762
    col_id_t* id = taosArrayGet(cols, i);
2,310✔
1763
    STREAM_CHECK_NULL_GOTO(id, terrno);
2,310!
1764
    for (size_t i = 0; i < schemaLen; i++) {
5,946!
1765
      SSchema* s = taosArrayGet(schemas, i);
5,946✔
1766
      STREAM_CHECK_NULL_GOTO(s, terrno);
5,946!
1767
      if (*id == s->colId) {
5,946✔
1768
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
2,310!
1769
        break;
2,310✔
1770
      }
1771
    }
1772
  }
1773
  taosArrayPopFrontBatch(schemas, schemaLen);
821✔
1774

1775
end:
821✔
1776
  return code;
821✔
1777
}
1778

1779
static int32_t processWalVerDataVTable(SVnode* pVnode, SArray *cids, int64_t ver,
×
1780
  int64_t uid, STimeWindow* window, SSDataBlock** pBlock) {
1781
  int32_t      code = 0;
×
1782
  int32_t      lino = 0;
×
1783
  SArray*      schemas = NULL;
×
1784

1785
  SSDataBlock* pBlock2 = NULL;
×
1786

1787
  STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, uid, &schemas));
×
1788
  STREAM_CHECK_RET_GOTO(shrinkScheams(cids, schemas));
×
1789
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlock2));
×
1790

1791
  pBlock2->info.id.uid = uid;
×
1792

1793
  // STREAM_CHECK_RET_GOTO(scanWalOneVer(pVnode, pBlock2, ver, uid, window));
1794
  //printDataBlock(pBlock2, __func__, "");
1795

1796
  *pBlock = pBlock2;
×
1797
  pBlock2 = NULL;
×
1798

1799
end:
×
1800
  STREAM_PRINT_LOG_END(code, lino);
×
1801
  blockDataDestroy(pBlock2);
×
1802
  taosArrayDestroy(schemas);
×
1803
  return code;
×
1804
}
1805

1806
static int32_t createTSAndCondition(int64_t start, int64_t end, SLogicConditionNode** pCond,
×
1807
                                    STargetNode* pTargetNodeTs) {
1808
  int32_t code = 0;
×
1809
  int32_t lino = 0;
×
1810

1811
  SColumnNode*         pCol = NULL;
×
1812
  SColumnNode*         pCol1 = NULL;
×
1813
  SValueNode*          pVal = NULL;
×
1814
  SValueNode*          pVal1 = NULL;
×
1815
  SOperatorNode*       op = NULL;
×
1816
  SOperatorNode*       op1 = NULL;
×
1817
  SLogicConditionNode* cond = NULL;
×
1818

1819
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol));
×
1820
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
×
1821
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
×
1822
  pCol->node.resType.bytes = LONG_BYTES;
×
1823
  pCol->slotId = pTargetNodeTs->slotId;
×
1824
  pCol->dataBlockId = pTargetNodeTs->dataBlockId;
×
1825

1826
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pCol, (SNode**)&pCol1));
×
1827

1828
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal));
×
1829
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
1830
  pVal->node.resType.bytes = LONG_BYTES;
×
1831
  pVal->datum.i = start;
×
1832
  pVal->typeData = start;
×
1833

1834
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pVal, (SNode**)&pVal1));
×
1835
  pVal1->datum.i = end;
×
1836
  pVal1->typeData = end;
×
1837

1838
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op));
×
1839
  op->opType = OP_TYPE_GREATER_EQUAL;
×
1840
  op->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1841
  op->node.resType.bytes = CHAR_BYTES;
×
1842
  op->pLeft = (SNode*)pCol;
×
1843
  op->pRight = (SNode*)pVal;
×
1844
  pCol = NULL;
×
1845
  pVal = NULL;
×
1846

1847
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op1));
×
1848
  op1->opType = OP_TYPE_LOWER_EQUAL;
×
1849
  op1->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1850
  op1->node.resType.bytes = CHAR_BYTES;
×
1851
  op1->pLeft = (SNode*)pCol1;
×
1852
  op1->pRight = (SNode*)pVal1;
×
1853
  pCol1 = NULL;
×
1854
  pVal1 = NULL;
×
1855

1856
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
×
1857
  cond->condType = LOGIC_COND_TYPE_AND;
×
1858
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1859
  cond->node.resType.bytes = CHAR_BYTES;
×
1860
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
×
1861
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op));
×
1862
  op = NULL;
×
1863
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op1));
×
1864
  op1 = NULL;
×
1865

1866
  *pCond = cond;
×
1867

1868
end:
×
1869
  if (code != 0) {
×
1870
    nodesDestroyNode((SNode*)pCol);
×
1871
    nodesDestroyNode((SNode*)pCol1);
×
1872
    nodesDestroyNode((SNode*)pVal);
×
1873
    nodesDestroyNode((SNode*)pVal1);
×
1874
    nodesDestroyNode((SNode*)op);
×
1875
    nodesDestroyNode((SNode*)op1);
×
1876
    nodesDestroyNode((SNode*)cond);
×
1877
  }
1878
  STREAM_PRINT_LOG_END(code, lino);
×
1879

1880
  return code;
×
1881
}
1882

1883
/*
1884
static int32_t createExternalConditions(SStreamRuntimeFuncInfo* data, SLogicConditionNode** pCond, STargetNode* pTargetNodeTs, STimeRangeNode* node) {
1885
  int32_t              code = 0;
1886
  int32_t              lino = 0;
1887
  SLogicConditionNode* pAndCondition = NULL;
1888
  SLogicConditionNode* cond = NULL;
1889

1890
  if (pTargetNodeTs == NULL) {
1891
    vError("stream reader %s no ts column", __func__);
1892
    return TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN;
1893
  }
1894
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
1895
  cond->condType = LOGIC_COND_TYPE_OR;
1896
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
1897
  cond->node.resType.bytes = CHAR_BYTES;
1898
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
1899

1900
  for (int i = 0; i < taosArrayGetSize(data->pStreamPesudoFuncVals); ++i) {
1901
    data->curIdx = i;
1902

1903
    SReadHandle handle = {0};
1904
    calcTimeRange(node, data, &handle.winRange, &handle.winRangeValid);
1905
    if (!handle.winRangeValid) {
1906
      stError("stream reader %s invalid time range, skey:%" PRId64 ", ekey:%" PRId64, __func__, handle.winRange.skey,
1907
              handle.winRange.ekey);
1908
      continue;
1909
    }
1910
    STREAM_CHECK_RET_GOTO(createTSAndCondition(handle.winRange.skey, handle.winRange.ekey, &pAndCondition, pTargetNodeTs));
1911
    stDebug("%s create condition skey:%" PRId64 ", eksy:%" PRId64, __func__, handle.winRange.skey, handle.winRange.ekey);
1912
    STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)pAndCondition));
1913
    pAndCondition = NULL;
1914
  }
1915

1916
  *pCond = cond;
1917

1918
end:
1919
  if (code != 0) {
1920
    nodesDestroyNode((SNode*)pAndCondition);
1921
    nodesDestroyNode((SNode*)cond);
1922
  }
1923
  STREAM_PRINT_LOG_END(code, lino);
1924

1925
  return code;
1926
}
1927
*/
1928

1929
static int32_t processCalaTimeRange(SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo, SResFetchReq* req,
985✔
1930
                                    STimeRangeNode* node, SReadHandle* handle) {
1931
  int32_t code = 0;
985✔
1932
  int32_t lino = 0;
985✔
1933
  SArray* funcVals = NULL;
985✔
1934
  if (req->pStRtFuncInfo->withExternalWindow) {
985✔
1935
/*
1936
    nodesDestroyNode(sStreamReaderCalcInfo->tsConditions);
1937
    filterFreeInfo(sStreamReaderCalcInfo->pFilterInfo);
1938
    sStreamReaderCalcInfo->pFilterInfo = NULL;
1939

1940
    STREAM_CHECK_RET_GOTO(createExternalConditions(req->pStRtFuncInfo,
1941
                                                   (SLogicConditionNode**)&sStreamReaderCalcInfo->tsConditions,
1942
                                                   sStreamReaderCalcInfo->pTargetNodeTs, node));
1943

1944
    STREAM_CHECK_RET_GOTO(filterInitFromNode((SNode*)sStreamReaderCalcInfo->tsConditions,
1945
                                             (SFilterInfo**)&sStreamReaderCalcInfo->pFilterInfo,
1946
                                             FLT_OPTION_NO_REWRITE | FLT_OPTION_SCALAR_MODE, NULL));
1947
*/                                             
1948
    sStreamReaderCalcInfo->tmpRtFuncInfo.curIdx = 0;
340✔
1949
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
340✔
1950
    
1951
    SSTriggerCalcParam* pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
340✔
1952
    SSTriggerCalcParam* pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
340✔
1953
    STREAM_CHECK_NULL_GOTO(pFirst, terrno);
340!
1954
    STREAM_CHECK_NULL_GOTO(pLast, terrno);
340!
1955

1956
    if (!node->needCalc) {
340✔
1957
      handle->winRange.skey = pFirst->wstart;
234✔
1958
      handle->winRange.ekey = pLast->wend;
234✔
1959
      handle->winRangeValid = true;
234✔
1960
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
234✔
1961
        handle->winRange.ekey--;
128✔
1962
      }
1963
    } else {
1964
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
106✔
1965
      memcpy(pTmp, pFirst, sizeof(*pTmp));
106✔
1966

1967
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, &handle->winRange, &handle->winRangeValid, 1));
106!
1968
      if (handle->winRangeValid) {
106!
1969
        int64_t skey = handle->winRange.skey;
106✔
1970

1971
        memcpy(pTmp, pLast, sizeof(*pTmp));
106✔
1972
        STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, &handle->winRange, &handle->winRangeValid, 2));
106!
1973

1974
        if (handle->winRangeValid) {
106!
1975
          handle->winRange.skey = skey;
106✔
1976
        }
1977
      }
1978
      handle->winRange.ekey--;
106✔
1979
    }
1980
  } else {
1981
    if (!node->needCalc) {
645✔
1982
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
165✔
1983
      handle->winRange.skey = pCurr->wstart;
165✔
1984
      handle->winRange.ekey = pCurr->wend;
165✔
1985
      handle->winRangeValid = true;
165✔
1986
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
165✔
1987
        handle->winRange.ekey--;
153✔
1988
      }
1989
    } else {
1990
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, &handle->winRange, &handle->winRangeValid, 3));
480!
1991
      handle->winRange.ekey--;
480✔
1992
    }
1993
  }
1994

1995
  stDebug("%s withExternalWindow is %d, skey:%" PRId64 ", ekey:%" PRId64 ", validRange:%d", 
985✔
1996
      __func__, req->pStRtFuncInfo->withExternalWindow, handle->winRange.skey, handle->winRange.ekey, handle->winRangeValid);
1997

1998
end:
32✔
1999
  taosArrayDestroy(funcVals);
985✔
2000
  return code;
985✔
2001
}
2002

2003
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,057✔
2004
                                  SStreamReaderTaskInner* pTaskInner) {
2005
  int32_t code = 0;
1,057✔
2006
  int32_t lino = 0;
1,057✔
2007

2008
  void* pTask = sStreamReaderInfo->pTask;
1,057✔
2009
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(pTaskInner->pTableList), sizeof(STsInfo));
1,057✔
2010
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
1,057!
2011
  while (true) {
2,120✔
2012
    bool hasNext = false;
3,177✔
2013
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
3,177!
2014
    if (hasNext) {
3,177✔
2015
      pTaskInner->api.tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
981✔
2016
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
981✔
2017
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
981!
2018
      if (pTaskInner->options.order == TSDB_ORDER_ASC) {
981✔
2019
        tsInfo->ts = pTaskInner->pResBlock->info.window.skey;
558✔
2020
      } else {
2021
        tsInfo->ts = pTaskInner->pResBlock->info.window.ekey;
423✔
2022
      }
2023
      tsInfo->gId = (sStreamReaderInfo->groupByTbname || sStreamReaderInfo->tableType != TSDB_SUPER_TABLE) ? 
129✔
2024
                    pTaskInner->pResBlock->info.id.uid : qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
1,110✔
2025
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, TD_VID(pVnode), __func__, tsInfo->ts,
981✔
2026
              tsInfo->gId, tsRsp->ver);
2027
    }
2028
    
2029
    pTaskInner->currentGroupIndex++;
3,177✔
2030
    if (pTaskInner->currentGroupIndex >= qStreamGetTableListGroupNum(pTaskInner->pTableList) || pTaskInner->options.gid != 0) {
3,177✔
2031
      break;
2032
    }
2033
    STREAM_CHECK_RET_GOTO(resetTsdbReader(pTaskInner));
2,120!
2034
  }
2035

2036
end:
1,057✔
2037
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,057!
2038
  return code;
1,057✔
2039
}
2040

2041
static int32_t vnodeProcessStreamSetTableReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
240✔
2042
  int32_t code = 0;
240✔
2043
  int32_t lino = 0;
240✔
2044
  void*   buf = NULL;
240✔
2045
  size_t  size = 0;
240✔
2046
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
240!
2047
  void* pTask = sStreamReaderInfo->pTask;
240✔
2048

2049
  ST_TASK_DLOG("vgId:%d %s start, trigger hash size:%d, calc hash size:%d", TD_VID(pVnode), __func__,
240✔
2050
                tSimpleHashGetSize(req->setTableReq.uidInfoTrigger), tSimpleHashGetSize(req->setTableReq.uidInfoCalc));
2051

2052
  TSWAP(sStreamReaderInfo->uidHashTrigger, req->setTableReq.uidInfoTrigger);
240✔
2053
  TSWAP(sStreamReaderInfo->uidHashCalc, req->setTableReq.uidInfoCalc);
240✔
2054
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashTrigger, TSDB_CODE_INVALID_PARA);
240!
2055
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashCalc, TSDB_CODE_INVALID_PARA);
240!
2056

2057
  sStreamReaderInfo->isVtableStream = true;
240✔
2058
  sStreamReaderInfo->groupByTbname = true;
240✔
2059
end:
240✔
2060
  STREAM_PRINT_LOG_END_WITHID(code, lino);
240!
2061
  SRpcMsg rsp = {
240✔
2062
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2063
  tmsgSendRsp(&rsp);
240✔
2064
  return code;
240✔
2065
}
2066

2067
static int32_t vnodeProcessStreamLastTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
597✔
2068
  int32_t                 code = 0;
597✔
2069
  int32_t                 lino = 0;
597✔
2070
  SStreamReaderTaskInner* pTaskInner = NULL;
597✔
2071
  SStreamTsResponse       lastTsRsp = {0};
597✔
2072
  void*                   buf = NULL;
597✔
2073
  size_t                  size = 0;
597✔
2074

2075
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
597!
2076
  void* pTask = sStreamReaderInfo->pTask;
597✔
2077

2078
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
597✔
2079

2080
  BUILD_OPTION(options, sStreamReaderInfo, -1, TSDB_ORDER_DESC, INT64_MIN, INT64_MAX, sStreamReaderInfo->tsSchemas, true,
597✔
2081
               STREAM_SCAN_GROUP_ONE_BY_ONE, 0, true, sStreamReaderInfo->uidHashTrigger);
2082
  SStorageAPI api = {0};
597✔
2083
  initStorageAPI(&api);
597✔
2084
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
597!
2085

2086
  lastTsRsp.ver = pVnode->state.applied + 1;
597✔
2087

2088
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &lastTsRsp, sStreamReaderInfo, pTaskInner));
597!
2089
  ST_TASK_DLOG("vgId:%d %s get result, ver:%" PRId64, TD_VID(pVnode), __func__, lastTsRsp.ver);
597✔
2090
  STREAM_CHECK_RET_GOTO(buildTsRsp(&lastTsRsp, &buf, &size))
597!
2091
  if (stDebugFlag & DEBUG_DEBUG) {
597✔
2092
    int32_t nInfo = taosArrayGetSize(lastTsRsp.tsInfo);
479✔
2093
    for (int32_t i = 0; i < nInfo; i++) {
862✔
2094
      STsInfo* tsInfo = TARRAY_GET_ELEM(lastTsRsp.tsInfo, i);
383✔
2095
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64, TD_VID(pVnode), __func__, tsInfo->ts, tsInfo->gId);
383!
2096
    }
2097
  }
2098

2099
end:
597✔
2100
  STREAM_PRINT_LOG_END_WITHID(code, lino);
597!
2101
  SRpcMsg rsp = {
597✔
2102
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2103
  tmsgSendRsp(&rsp);
597✔
2104
  taosArrayDestroy(lastTsRsp.tsInfo);
597✔
2105
  releaseStreamTask(&pTaskInner);
597✔
2106
  return code;
597✔
2107
}
2108

2109
static int32_t vnodeProcessStreamFirstTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
472✔
2110
  int32_t                 code = 0;
472✔
2111
  int32_t                 lino = 0;
472✔
2112
  SStreamReaderTaskInner* pTaskInner = NULL;
472✔
2113
  SStreamTsResponse       firstTsRsp = {0};
472✔
2114
  void*                   buf = NULL;
472✔
2115
  size_t                  size = 0;
472✔
2116

2117
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
472!
2118
  void* pTask = sStreamReaderInfo->pTask;
472✔
2119
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64, TD_VID(pVnode), __func__, req->firstTsReq.startTime, req->firstTsReq.ver, req->firstTsReq.gid);
472✔
2120
  BUILD_OPTION(options, sStreamReaderInfo, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, sStreamReaderInfo->tsSchemas, true,
472✔
2121
               STREAM_SCAN_GROUP_ONE_BY_ONE, req->firstTsReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2122
  SStorageAPI api = {0};
472✔
2123
  initStorageAPI(&api);
472✔
2124
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
472✔
2125
  
2126
  firstTsRsp.ver = pVnode->state.applied;
460✔
2127
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &firstTsRsp, sStreamReaderInfo, pTaskInner));
460!
2128

2129
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(firstTsRsp.tsInfo), firstTsRsp.ver);
460✔
2130
  STREAM_CHECK_RET_GOTO(buildTsRsp(&firstTsRsp, &buf, &size));
460!
2131
  if (stDebugFlag & DEBUG_DEBUG) {
460✔
2132
    int32_t nInfo = taosArrayGetSize(firstTsRsp.tsInfo);
333✔
2133
    for (int32_t i = 0; i < nInfo; i++) {
730✔
2134
      STsInfo* tsInfo = TARRAY_GET_ELEM(firstTsRsp.tsInfo, i);
397✔
2135
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64, TD_VID(pVnode), __func__, tsInfo->ts, tsInfo->gId);
397!
2136
    }
2137
  }
2138

2139
end:
460✔
2140
  STREAM_PRINT_LOG_END_WITHID(code, lino);
472!
2141
  SRpcMsg rsp = {
472✔
2142
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2143
  tmsgSendRsp(&rsp);
472✔
2144
  taosArrayDestroy(firstTsRsp.tsInfo);
472✔
2145
  releaseStreamTask(&pTaskInner);
472✔
2146
  return code;
472✔
2147
}
2148

2149
static int32_t vnodeProcessStreamTsdbMetaReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
992✔
2150
  int32_t code = 0;
992✔
2151
  int32_t lino = 0;
992✔
2152
  void*   buf = NULL;
992✔
2153
  size_t  size = 0;
992✔
2154

2155
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
992!
2156
  void* pTask = sStreamReaderInfo->pTask;
992✔
2157
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
992✔
2158

2159
  SStreamReaderTaskInner* pTaskInner = NULL;
992✔
2160
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_META);
992✔
2161

2162
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
992!
2163
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, sStreamReaderInfo->tsSchemas, true, 
992✔
2164
      (req->tsdbMetaReq.gid != 0 ? STREAM_SCAN_GROUP_ONE_BY_ONE : STREAM_SCAN_ALL), req->tsdbMetaReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2165
    SStorageAPI api = {0};
992✔
2166
    initStorageAPI(&api);
992✔
2167
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
991✔
2168
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
980!
2169
    
2170
    STREAM_CHECK_RET_GOTO(createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream));
980!
2171
  } else {
2172
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2173
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2174
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2175
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2176
  }
2177

2178
  blockDataCleanup(pTaskInner->pResBlockDst);
980✔
2179
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
980!
2180
  bool hasNext = true;
980✔
2181
  while (true) {
409✔
2182
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,389!
2183
    if (!hasNext) {
1,389✔
2184
      break;
980✔
2185
    }
2186
    pTaskInner->api.tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
409✔
2187
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
409✔
2188

2189
    int32_t index = 0;
409✔
2190
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.skey));
409!
2191
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.ekey));
409!
2192
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.uid));
409!
2193
    if (!sStreamReaderInfo->isVtableStream) {
409✔
2194
      STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.groupId));
76!
2195
    }
2196
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.rows));
409!
2197

2198
    stDebug("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
409✔
2199
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2200
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2201
            pTaskInner->pResBlockDst->info.rows++;
409✔
2202
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
409!
2203
      break;
×
2204
    }
2205
  }
2206

2207
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
980✔
2208
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
980!
2209
  printDataBlock(pTaskInner->pResBlockDst, __func__, "meta", ((SStreamTask *)sStreamReaderInfo->pTask)->streamId);
980✔
2210
  if (!hasNext) {
980!
2211
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
980!
2212
  }
2213

2214
end:
980✔
2215
  STREAM_PRINT_LOG_END_WITHID(code, lino);
992!
2216
  SRpcMsg rsp = {
992✔
2217
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2218
  tmsgSendRsp(&rsp);
992✔
2219
  return code;
992✔
2220
}
2221

2222
static int32_t vnodeProcessStreamTsdbTsDataReqNonVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
43✔
2223
  int32_t                 code = 0;
43✔
2224
  int32_t                 lino = 0;
43✔
2225
  SStreamReaderTaskInner* pTaskInner = NULL;
43✔
2226
  void*                   buf = NULL;
43✔
2227
  size_t                  size = 0;
43✔
2228
  SSDataBlock*            pBlockRes = NULL;
43✔
2229

2230
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
43!
2231
  void* pTask = sStreamReaderInfo->pTask;
43✔
2232
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
43!
2233
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
2234
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
2235

2236
  BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
43✔
2237
               sStreamReaderInfo->triggerCols, false, STREAM_SCAN_ALL, 0, true, NULL);
2238
  options.uid = req->tsdbTsDataReq.uid;
43✔
2239
  SStorageAPI api = {0};
43✔
2240
  initStorageAPI(&api);
43✔
2241
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
43!
2242
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
43!
2243
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
43!
2244

2245
  while (1) {
43✔
2246
    bool hasNext = false;
86✔
2247
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
86!
2248
    if (!hasNext) {
86✔
2249
      break;
43✔
2250
    }
2251
    if (!sStreamReaderInfo->isVtableStream){
43!
2252
      pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
43✔
2253
    }
2254

2255
    SSDataBlock* pBlock = NULL;
43✔
2256
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
43!
2257
    if (pBlock != NULL && pBlock->info.rows > 0) {
43!
2258
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, false, &api, pBlock->info.id.uid, pBlock,
43!
2259
          0, pBlock->info.rows, 1));
2260
    }
2261
    
2262
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
43!
2263
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
43!
2264
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
43!
2265
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2266
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2267
  }
2268

2269
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
43✔
2270

2271
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
43!
2272
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
43!
2273

2274
end:
43✔
2275
  STREAM_PRINT_LOG_END_WITHID(code, lino);
43!
2276
  SRpcMsg rsp = {
43✔
2277
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2278
  tmsgSendRsp(&rsp);
43✔
2279
  blockDataDestroy(pBlockRes);
43✔
2280

2281
  releaseStreamTask(&pTaskInner);
43✔
2282
  return code;
43✔
2283
}
2284

2285
static int32_t vnodeProcessStreamTsdbTsDataReqVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1✔
2286
  int32_t                 code = 0;
1✔
2287
  int32_t                 lino = 0;
1✔
2288
  SStreamReaderTaskInner* pTaskInner = NULL;
1✔
2289
  void*                   buf = NULL;
1✔
2290
  size_t                  size = 0;
1✔
2291
  SSDataBlock*            pBlockRes = NULL;
1✔
2292

2293
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
1!
2294
  void* pTask = sStreamReaderInfo->pTask;
1✔
2295
  ST_TASK_ELOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
1!
2296
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
2297
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
2298

2299
  BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
1✔
2300
               sStreamReaderInfo->tsSchemas, true, STREAM_SCAN_ALL, 0, true, NULL);
2301
  options.suid = req->tsdbTsDataReq.suid;
1✔
2302
  options.uid = req->tsdbTsDataReq.uid;
1✔
2303
  SStorageAPI api = {0};
1✔
2304
  initStorageAPI(&api);
1✔
2305
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->tsBlock, &api));
1!
2306
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
1!
2307

2308
  while (1) {
1✔
2309
    bool hasNext = false;
2✔
2310
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
2!
2311
    if (!hasNext) {
2✔
2312
      break;
1✔
2313
    }
2314

2315
    SSDataBlock* pBlock = NULL;
1✔
2316
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
1!
2317
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
1!
2318
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
1!
2319
            TD_VID(pVnode), __func__, pBlockRes->info.window.skey, pBlockRes->info.window.ekey,
2320
            pBlockRes->info.id.uid, pBlockRes->info.id.groupId, pBlockRes->info.rows);
2321
  }
2322

2323
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
1!
2324
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
1!
2325

2326
end:
1✔
2327
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1!
2328
  SRpcMsg rsp = {
1✔
2329
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2330
  tmsgSendRsp(&rsp);
1✔
2331
  blockDataDestroy(pBlockRes);
1✔
2332

2333
  releaseStreamTask(&pTaskInner);
1✔
2334
  return code;
1✔
2335
}
2336

2337
static int32_t vnodeProcessStreamTsdbTriggerDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
293✔
2338
  int32_t code = 0;
293✔
2339
  int32_t lino = 0;
293✔
2340
  void*   buf = NULL;
293✔
2341
  size_t  size = 0;
293✔
2342

2343
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
293!
2344
  SStreamReaderTaskInner* pTaskInner = NULL;
293✔
2345
  void* pTask = sStreamReaderInfo->pTask;
293✔
2346
  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);
293✔
2347
  
2348
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
293✔
2349

2350
  if (req->base.type == STRIGGER_PULL_TSDB_TRIGGER_DATA) {
293✔
2351
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, INT64_MAX,
141✔
2352
                 sStreamReaderInfo->triggerCols, false, (req->tsdbTriggerDataReq.gid != 0 ? STREAM_SCAN_GROUP_ONE_BY_ONE : STREAM_SCAN_ALL), 
2353
                 req->tsdbTriggerDataReq.gid, true, NULL);
2354
    SStorageAPI api = {0};
141✔
2355
    initStorageAPI(&api);
141✔
2356
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
141!
2357

2358
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
141!
2359
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
141!
2360
  } else {
2361
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
152✔
2362
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
152!
2363
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
152✔
2364
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
152!
2365
  }
2366

2367
  blockDataCleanup(pTaskInner->pResBlockDst);
293✔
2368
  bool hasNext = true;
293✔
2369
  while (1) {
×
2370
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
293!
2371
    if (!hasNext) {
292✔
2372
      break;
141✔
2373
    }
2374
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
151✔
2375
    pTaskInner->pResBlockDst->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
152✔
2376

2377
    SSDataBlock* pBlock = NULL;
152✔
2378
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
152!
2379
    if (pBlock != NULL && pBlock->info.rows > 0) {
152!
2380
      STREAM_CHECK_RET_GOTO(
152!
2381
        processTag(pVnode, sStreamReaderInfo, false, &pTaskInner->api, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
2382
    }
2383
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
152!
2384
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
152!
2385
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
152✔
2386
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2387
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2388
    if (pTaskInner->pResBlockDst->info.rows >= 0) { //todo
152!
2389
      break;
152✔
2390
    }
2391
  }
2392

2393
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
293!
2394
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
293✔
2395
  if (!hasNext) {
293✔
2396
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
141!
2397
  }
2398

2399
end:
293✔
2400
  STREAM_PRINT_LOG_END_WITHID(code, lino);
293!
2401
  SRpcMsg rsp = {
293✔
2402
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2403
  tmsgSendRsp(&rsp);
293✔
2404

2405
  return code;
293✔
2406
}
2407

2408
static int32_t vnodeProcessStreamTsdbCalcDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
28,790✔
2409
  int32_t code = 0;
28,790✔
2410
  int32_t lino = 0;
28,790✔
2411
  void*   buf = NULL;
28,790✔
2412
  size_t  size = 0;
28,790✔
2413
  SSDataBlock*            pBlockRes = NULL;
28,790✔
2414

2415
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
28,790!
2416
  void* pTask = sStreamReaderInfo->pTask;
28,790✔
2417
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64, TD_VID(pVnode), __func__, 
28,790✔
2418
    req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey, req->tsdbCalcDataReq.gid);
2419

2420
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerCols, TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN);
28,790!
2421

2422
  SStreamReaderTaskInner* pTaskInner = NULL;
28,790✔
2423
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
28,790✔
2424

2425
  if (req->base.type == STRIGGER_PULL_TSDB_CALC_DATA) {
28,790✔
2426
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbCalcDataReq.ver, TSDB_ORDER_ASC, req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey,
28,789✔
2427
                 sStreamReaderInfo->triggerCols, false, STREAM_SCAN_GROUP_ONE_BY_ONE, req->tsdbCalcDataReq.gid, true, NULL);
2428
    SStorageAPI api = {0};
28,789✔
2429
    initStorageAPI(&api);
28,789✔
2430
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
28,788✔
2431

2432
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
28,438!
2433
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
28,438!
2434
  } else {
2435
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
1✔
2436
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2437
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2438
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2439
  }
2440

2441
  blockDataCleanup(pTaskInner->pResBlockDst);
28,439✔
2442
  bool hasNext = true;
28,439✔
2443
  while (1) {
2,739✔
2444
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
31,178!
2445
    if (!hasNext) {
31,178✔
2446
      break;
28,437✔
2447
    }
2448
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
2,741✔
2449

2450
    SSDataBlock* pBlock = NULL;
2,742✔
2451
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
2,742!
2452
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
2,741!
2453
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
2,741!
2454
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
2,739!
2455
      break;
×
2456
    }
2457
  }
2458

2459
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
28,437!
2460
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
28,433!
2461
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
28,432✔
2462
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
28,437!
2463
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
28,436✔
2464
  if (!hasNext) {
28,439!
2465
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
28,439!
2466
  }
2467

2468
end:
28,439✔
2469
  STREAM_PRINT_LOG_END_WITHID(code, lino);
28,790!
2470
  SRpcMsg rsp = {
28,790✔
2471
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2472
  tmsgSendRsp(&rsp);
28,790✔
2473
  blockDataDestroy(pBlockRes);
28,789✔
2474
  return code;
28,786✔
2475
}
2476

2477
static int32_t vnodeProcessStreamTsdbVirtalDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
823✔
2478
  int32_t code = 0;
823✔
2479
  int32_t lino = 0;
823✔
2480
  void*   buf = NULL;
823✔
2481
  size_t  size = 0;
823✔
2482
  int32_t* slotIdList = NULL;
823✔
2483
  SArray* sortedCid = NULL;
823✔
2484
  SArray* schemas = NULL;
823✔
2485
  
2486
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
823!
2487
  void* pTask = sStreamReaderInfo->pTask;
823✔
2488
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
823✔
2489

2490
  SStreamReaderTaskInner* pTaskInner = NULL;
823✔
2491
  int64_t key = req->tsdbDataReq.uid;
823✔
2492

2493
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
823!
2494
    // sort cid and build slotIdList
2495
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
823!
2496
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
825!
2497
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
823✔
2498
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
823!
2499
    taosArraySort(sortedCid, sortCid);
823✔
2500
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
3,139✔
2501
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
2,316✔
2502
      STREAM_CHECK_NULL_GOTO(cid, terrno);
2,316!
2503
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
4,488!
2504
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
4,488✔
2505
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
4,488!
2506
        if (*cid == *cidSorted) {
4,488✔
2507
          slotIdList[j] = i;
2,316✔
2508
          break;
2,316✔
2509
        }
2510
      }
2511
    }
2512

2513
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, req->tsdbDataReq.uid, &schemas));
823✔
2514
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
821!
2515
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
821✔
2516
                    req->tsdbDataReq.ekey, schemas, true, STREAM_SCAN_ALL, 0, false, NULL);
2517

2518
    options.suid = req->tsdbDataReq.suid;
821✔
2519
    options.uid = req->tsdbDataReq.uid;
821✔
2520

2521
    SStorageAPI api = {0};
821✔
2522
    initStorageAPI(&api);
821✔
2523
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
821!
2524
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
821!
2525

2526
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid};
821✔
2527
    cleanupQueryTableDataCond(&pTaskInner->cond);
821✔
2528
    taosArraySort(pTaskInner->options.schemas, sortSSchema);
821✔
2529

2530
    STREAM_CHECK_RET_GOTO(qStreamInitQueryTableDataCond(&pTaskInner->cond, pTaskInner->options.order, pTaskInner->options.schemas,
821!
2531
                                                        pTaskInner->options.isSchema, pTaskInner->options.twindows,
2532
                                                        pTaskInner->options.suid, pTaskInner->options.ver, &slotIdList));
2533
    STREAM_CHECK_RET_GOTO(pTaskInner->api.tsdReader.tsdReaderOpen(pVnode, &pTaskInner->cond, &keyInfo, 1, pTaskInner->pResBlock,
821!
2534
                                                             (void**)&pTaskInner->pReader, pTaskInner->idStr, NULL));
2535
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pTaskInner->pResBlock, false, &pTaskInner->pResBlockDst));
821!
2536
  } else {
2537
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2538
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2539
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2540
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2541
  }
2542

2543
  blockDataCleanup(pTaskInner->pResBlockDst);
821✔
2544
  bool hasNext = true;
821✔
2545
  while (1) {
821✔
2546
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,642!
2547
    if (!hasNext) {
1,642✔
2548
      break;
821✔
2549
    }
2550

2551
    SSDataBlock* pBlock = NULL;
821✔
2552
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
821!
2553
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
821!
2554
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
821!
2555
      break;
×
2556
    }
2557
  }
2558
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
821!
2559
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
821✔
2560
  printDataBlock(pTaskInner->pResBlockDst, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
821✔
2561
  if (!hasNext) {
821!
2562
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
821!
2563
  }
2564

2565
end:
821✔
2566
  STREAM_PRINT_LOG_END_WITHID(code, lino);
823!
2567
  SRpcMsg rsp = {
823✔
2568
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2569
  tmsgSendRsp(&rsp);
823✔
2570
  taosMemFree(slotIdList);
823✔
2571
  taosArrayDestroy(sortedCid);
823✔
2572
  taosArrayDestroy(schemas);
823✔
2573
  return code;
823✔
2574
}
2575

2576
static int32_t vnodeProcessStreamWalMetaNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
13,796✔
2577
  int32_t      code = 0;
13,796✔
2578
  int32_t      lino = 0;
13,796✔
2579
  void*        buf = NULL;
13,796✔
2580
  size_t       size = 0;
13,796✔
2581
  int64_t      lastVer = 0;
13,796✔
2582
  SSTriggerWalNewRsp resultRsp = {0};
13,796✔
2583

2584
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
13,796✔
2585
  void* pTask = sStreamReaderInfo->pTask;
13,738✔
2586
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaNewReq.lastVer);
13,738✔
2587

2588
  if (sStreamReaderInfo->metaBlock == NULL) {
13,739✔
2589
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
432!
2590
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
432!
2591
  }
2592
  blockDataEmpty(sStreamReaderInfo->metaBlock);
13,739✔
2593
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
13,730✔
2594
  resultRsp.ver = req->walMetaNewReq.lastVer;
13,730✔
2595
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
13,730!
2596

2597
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
13,743✔
2598
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
13,741✔
2599
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, NULL);
480✔
2600
  buf = rpcMallocCont(size);
480✔
2601
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, NULL);
480✔
2602
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
480✔
2603

2604
end:
13,798✔
2605
  if (resultRsp.totalRows == 0) {
13,798✔
2606
    code = TSDB_CODE_STREAM_NO_DATA;
13,318✔
2607
    buf = rpcMallocCont(sizeof(int64_t));
13,318✔
2608
    *(int64_t *)buf = resultRsp.ver;
13,321✔
2609
    size = sizeof(int64_t);
13,321✔
2610
  }
2611
  SRpcMsg rsp = {
13,801✔
2612
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2613
  tmsgSendRsp(&rsp);
13,801✔
2614
  if (code == TSDB_CODE_STREAM_NO_DATA){
13,802✔
2615
    code = 0;
13,322✔
2616
  }
2617
  STREAM_PRINT_LOG_END_WITHID(code, lino);
13,802!
2618
  blockDataDestroy(resultRsp.deleteBlock);
13,801✔
2619
  blockDataDestroy(resultRsp.dropBlock);
13,799✔
2620

2621
  return code;
13,799✔
2622
}
2623
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
7,674✔
2624
  int32_t      code = 0;
7,674✔
2625
  int32_t      lino = 0;
7,674✔
2626
  void*        buf = NULL;
7,674✔
2627
  size_t       size = 0;
7,674✔
2628
  SSTriggerWalNewRsp resultRsp = {0};
7,674✔
2629
  
2630
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
7,674!
2631
  void* pTask = sStreamReaderInfo->pTask;
7,674✔
2632
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaDataNewReq.lastVer);
7,674✔
2633

2634
  if (sStreamReaderInfo->metaBlock == NULL) {
7,674✔
2635
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
176!
2636
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
176!
2637
  }
2638
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
7,674✔
2639
  resultRsp.dataBlock = sStreamReaderInfo->triggerBlock;
7,674✔
2640
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
7,674✔
2641
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp));
7,674!
2642

2643
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
7,669✔
2644
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
299✔
2645
  buf = rpcMallocCont(size);
298✔
2646
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
298✔
2647
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
298✔
2648
  printDataBlock(sStreamReaderInfo->triggerBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
298✔
2649
  printDataBlock(resultRsp.dropBlock, __func__, "drop", ((SStreamTask*)pTask)->streamId);
298✔
2650
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
298✔
2651
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
298✔
2652

2653
end:
7,667✔
2654
  if (resultRsp.totalRows == 0) {
7,667✔
2655
    buf = rpcMallocCont(sizeof(int64_t));
7,369✔
2656
    *(int64_t *)buf = resultRsp.ver;
7,372✔
2657
    size = sizeof(int64_t);
7,372✔
2658
    code = TSDB_CODE_STREAM_NO_DATA;
7,372✔
2659
  }
2660
  SRpcMsg rsp = {
7,670✔
2661
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2662
  tmsgSendRsp(&rsp);
7,670✔
2663
  if (code == TSDB_CODE_STREAM_NO_DATA){
7,666✔
2664
    code = 0;
7,368✔
2665
  }
2666
  blockDataDestroy(resultRsp.deleteBlock);
7,666✔
2667
  blockDataDestroy(resultRsp.dropBlock);
7,663✔
2668

2669
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,664!
2670

2671
  return code;
7,662✔
2672
}
2673

2674
static int32_t vnodeProcessStreamWalDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
7,101✔
2675
  int32_t      code = 0;
7,101✔
2676
  int32_t      lino = 0;
7,101✔
2677
  void*        buf = NULL;
7,101✔
2678
  size_t       size = 0;
7,101✔
2679
  SSTriggerWalNewRsp resultRsp = {0};
7,101✔
2680

2681
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
7,101!
2682
  void* pTask = sStreamReaderInfo->pTask;
7,101✔
2683
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
7,101✔
2684

2685
  resultRsp.dataBlock = sStreamReaderInfo->triggerBlock;
7,101✔
2686
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
7,101!
2687
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
7,101✔
2688

2689
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
7,100✔
2690

2691
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
275✔
2692
  buf = rpcMallocCont(size);
275✔
2693
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
275✔
2694
  printDataBlock(sStreamReaderInfo->triggerBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
275✔
2695
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
275✔
2696

2697
end:
7,100✔
2698
  if (resultRsp.totalRows == 0) {
7,100✔
2699
    buf = rpcMallocCont(sizeof(int64_t));
6,825✔
2700
    *(int64_t *)buf = resultRsp.ver;
6,826✔
2701
    size = sizeof(int64_t);
6,826✔
2702
    code = TSDB_CODE_STREAM_NO_DATA;
6,826✔
2703
  }
2704
  SRpcMsg rsp = {
7,101✔
2705
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2706
  tmsgSendRsp(&rsp);
7,101✔
2707
  if (code == TSDB_CODE_STREAM_NO_DATA){
7,100✔
2708
    code = 0;
6,825✔
2709
  }
2710

2711
  blockDataDestroy(resultRsp.deleteBlock);
7,100✔
2712
  blockDataDestroy(resultRsp.dropBlock);
7,099✔
2713
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,098!
2714

2715
  return code;
7,098✔
2716
}
2717

2718
static int32_t vnodeProcessStreamWalCalcDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,298✔
2719
  int32_t      code = 0;
1,298✔
2720
  int32_t      lino = 0;
1,298✔
2721
  void*        buf = NULL;
1,298✔
2722
  size_t       size = 0;
1,298✔
2723
  SSTriggerWalNewRsp resultRsp = {0};
1,298✔
2724
  SSDataBlock* pBlock1 = NULL;
1,298✔
2725
  SSDataBlock* pBlock2 = NULL;
1,298✔
2726
  
2727
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
1,298!
2728
  void* pTask = sStreamReaderInfo->pTask;
1,298✔
2729
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
1,298✔
2730

2731
  resultRsp.dataBlock = sStreamReaderInfo->isVtableStream ? sStreamReaderInfo->calcBlock : sStreamReaderInfo->triggerBlock;
1,298✔
2732
  resultRsp.isCalc = sStreamReaderInfo->isVtableStream ? true : false;
1,298✔
2733
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
1,298!
2734
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
1,298✔
2735

2736
  if (!sStreamReaderInfo->isVtableStream){
724✔
2737
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, true, &pBlock1));
504!
2738
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
504!
2739
  
2740
    blockDataTransform(pBlock2, pBlock1);
504✔
2741
    resultRsp.dataBlock = pBlock2;
504✔
2742
  }
2743

2744
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
724✔
2745
  buf = rpcMallocCont(size);
723✔
2746
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
724✔
2747
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
724✔
2748
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
723✔
2749

2750
end:
1,298✔
2751
  if (resultRsp.totalRows == 0) {
1,298✔
2752
    buf = rpcMallocCont(sizeof(int64_t));
574✔
2753
    *(int64_t *)buf = resultRsp.ver;
574✔
2754
    size = sizeof(int64_t);
574✔
2755
    code = TSDB_CODE_STREAM_NO_DATA;
574✔
2756
  }
2757
  SRpcMsg rsp = {
1,298✔
2758
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2759
  tmsgSendRsp(&rsp);
1,298✔
2760
  if (code == TSDB_CODE_STREAM_NO_DATA){
1,298✔
2761
    code = 0;
574✔
2762
  }
2763

2764
  blockDataDestroy(pBlock1);
1,298✔
2765
  blockDataDestroy(pBlock2);
1,298✔
2766
  blockDataDestroy(resultRsp.deleteBlock);
1,298✔
2767
  blockDataDestroy(resultRsp.dropBlock);
1,298✔
2768
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,298!
2769

2770
  return code;
1,298✔
2771
}
2772

2773
static int32_t vnodeProcessStreamGroupColValueReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
758✔
2774
  int32_t code = 0;
758✔
2775
  int32_t lino = 0;
758✔
2776
  void*   buf = NULL;
758✔
2777
  size_t  size = 0;
758✔
2778

2779
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
758!
2780
  void* pTask = sStreamReaderInfo->pTask;
758✔
2781
  ST_TASK_DLOG("vgId:%d %s start, request gid:%" PRId64, TD_VID(pVnode), __func__, req->groupColValueReq.gid);
758✔
2782

2783
  SArray** gInfo = taosHashGet(sStreamReaderInfo->groupIdMap, &req->groupColValueReq.gid, POINTER_BYTES);
758✔
2784
  STREAM_CHECK_NULL_GOTO(gInfo, TSDB_CODE_STREAM_NO_CONTEXT);
758!
2785
  SStreamGroupInfo pGroupInfo = {0};
758✔
2786
  pGroupInfo.gInfo = *gInfo;
758✔
2787

2788
  size = tSerializeSStreamGroupInfo(NULL, 0, &pGroupInfo, TD_VID(pVnode));
758✔
2789
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
2790
  buf = rpcMallocCont(size);
758✔
2791
  STREAM_CHECK_NULL_GOTO(buf, terrno);
758!
2792
  size = tSerializeSStreamGroupInfo(buf, size, &pGroupInfo, TD_VID(pVnode));
758✔
2793
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
2794
end:
758✔
2795
  if (code != 0) {
758!
2796
    rpcFreeCont(buf);
×
2797
    buf = NULL;
×
2798
    size = 0;
×
2799
  }
2800
  STREAM_PRINT_LOG_END_WITHID(code, lino);
758!
2801
  SRpcMsg rsp = {
758✔
2802
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2803
  tmsgSendRsp(&rsp);
758✔
2804

2805
  return code;
757✔
2806
}
2807

2808
static int32_t vnodeProcessStreamVTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
791✔
2809
  int32_t              code = 0;
791✔
2810
  int32_t              lino = 0;
791✔
2811
  void*                buf = NULL;
791✔
2812
  size_t               size = 0;
791✔
2813
  SStreamMsgVTableInfo vTableInfo = {0};
791✔
2814
  SMetaReader          metaReader = {0};
791✔
2815
  SStorageAPI api = {0};
791✔
2816
  initStorageAPI(&api);
791✔
2817

2818
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
790!
2819
  void* pTask = sStreamReaderInfo->pTask;
790✔
2820
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
790✔
2821

2822
  SArray* cids = req->virTableInfoReq.cids;
790✔
2823
  STREAM_CHECK_NULL_GOTO(cids, terrno);
790!
2824

2825
  SArray* pTableListArray = qStreamGetTableArrayList(sStreamReaderInfo->tableList);
790✔
2826
  STREAM_CHECK_NULL_GOTO(pTableListArray, terrno);
790!
2827

2828
  vTableInfo.infos = taosArrayInit(taosArrayGetSize(pTableListArray), sizeof(VTableInfo));
790✔
2829
  STREAM_CHECK_NULL_GOTO(vTableInfo.infos, terrno);
791!
2830
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
791✔
2831

2832
  for (size_t i = 0; i < taosArrayGetSize(pTableListArray); i++) {
2,476✔
2833
    STableKeyInfo* pKeyInfo = taosArrayGet(pTableListArray, i);
1,685✔
2834
    if (pKeyInfo == NULL) {
1,685!
2835
      continue;
×
2836
    }
2837
    VTableInfo* vTable = taosArrayReserve(vTableInfo.infos, 1);
1,685✔
2838
    STREAM_CHECK_NULL_GOTO(vTable, terrno);
1,685!
2839
    vTable->uid = pKeyInfo->uid;
1,685✔
2840
    vTable->gId = pKeyInfo->groupId;
1,685✔
2841

2842
    code = api.metaReaderFn.getTableEntryByUid(&metaReader, pKeyInfo->uid);
1,685✔
2843
    if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
1,685!
2844
      vTable->cols.nCols = metaReader.me.colRef.nCols;
108✔
2845
      vTable->cols.version = metaReader.me.colRef.version;
108✔
2846
      vTable->cols.pColRef = taosMemoryCalloc(metaReader.me.colRef.nCols, sizeof(SColRef));
108!
2847
      for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
648✔
2848
        memcpy(vTable->cols.pColRef + j, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
540✔
2849
      }
2850
    } else {
2851
      vTable->cols.nCols = taosArrayGetSize(cids);
1,576✔
2852
      vTable->cols.version = metaReader.me.colRef.version;
1,576✔
2853
      vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
1,576!
2854
      for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
5,776✔
2855
        for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
18,029✔
2856
          if (metaReader.me.colRef.pColRef[j].hasRef &&
16,453✔
2857
              metaReader.me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
12,168✔
2858
            memcpy(vTable->cols.pColRef + i, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
2,625✔
2859
            break;
2,625✔
2860
          }
2861
        }
2862
      }
2863
    }
2864
    tDecoderClear(&metaReader.coder);
1,683✔
2865
  }
2866
  ST_TASK_DLOG("vgId:%d %s end", TD_VID(pVnode), __func__);
791✔
2867
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
791!
2868

2869
end:
791✔
2870
  tDestroySStreamMsgVTableInfo(&vTableInfo);
791✔
2871
  api.metaReaderFn.clearReader(&metaReader);
791✔
2872
  STREAM_PRINT_LOG_END_WITHID(code, lino);
791!
2873
  SRpcMsg rsp = {
791✔
2874
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2875
  tmsgSendRsp(&rsp);
791✔
2876
  return code;
791✔
2877
}
2878

2879
static int32_t vnodeProcessStreamOTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req) {
240✔
2880
  int32_t                   code = 0;
240✔
2881
  int32_t                   lino = 0;
240✔
2882
  void*                     buf = NULL;
240✔
2883
  size_t                    size = 0;
240✔
2884
  SSTriggerOrigTableInfoRsp oTableInfo = {0};
240✔
2885
  SMetaReader               metaReader = {0};
240✔
2886
  int64_t streamId = req->base.streamId;
240✔
2887
  stsDebug("vgId:%d %s start", TD_VID(pVnode), __func__);
240✔
2888

2889
  SStorageAPI api = {0};
240✔
2890
  initStorageAPI(&api);
240✔
2891

2892
  SArray* cols = req->origTableInfoReq.cols;
240✔
2893
  STREAM_CHECK_NULL_GOTO(cols, terrno);
240!
2894

2895
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
240✔
2896

2897
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
240!
2898

2899
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
240✔
2900
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
819✔
2901
    OTableInfo*    oInfo = taosArrayGet(cols, i);
579✔
2902
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
579✔
2903
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
579!
2904
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
579!
2905
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByName(&metaReader, oInfo->refTableName));
579!
2906
    vTableInfo->uid = metaReader.me.uid;
577✔
2907
    stsDebug("vgId:%d %s uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
577✔
2908

2909
    SSchemaWrapper* sSchemaWrapper = NULL;
578✔
2910
    if (metaReader.me.type == TD_CHILD_TABLE) {
578✔
2911
      int64_t suid = metaReader.me.ctbEntry.suid;
574✔
2912
      vTableInfo->suid = suid;
574✔
2913
      tDecoderClear(&metaReader.coder);
574✔
2914
      STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, suid));
575!
2915
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
575✔
2916
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
4!
2917
      vTableInfo->suid = 0;
4✔
2918
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
4✔
2919
    } else {
2920
      stError("invalid table type:%d", metaReader.me.type);
×
2921
    }
2922

2923
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
2,100!
2924
      SSchema* s = sSchemaWrapper->pSchema + j;
2,100✔
2925
      if (strcmp(s->name, oInfo->refColName) == 0) {
2,100✔
2926
        vTableInfo->cid = s->colId;
579✔
2927
        break;
579✔
2928
      }
2929
    }
2930
    if (vTableInfo->cid == 0) {
579!
2931
      stError("vgId:%d %s, not found col %s in table %s", TD_VID(pVnode), __func__, oInfo->refColName,
×
2932
              oInfo->refTableName);
2933
    }
2934
    tDecoderClear(&metaReader.coder);
579✔
2935
  }
2936

2937
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
239!
2938

2939
end:
240✔
2940
  tDestroySTriggerOrigTableInfoRsp(&oTableInfo);
240✔
2941
  api.metaReaderFn.clearReader(&metaReader);
240✔
2942
  STREAM_PRINT_LOG_END(code, lino);
240!
2943
  SRpcMsg rsp = {
240✔
2944
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2945
  tmsgSendRsp(&rsp);
240✔
2946
  return code;
240✔
2947
}
2948

2949
static int32_t vnodeProcessStreamVTableTagInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req) {
1,387✔
2950
  int32_t                   code = 0;
1,387✔
2951
  int32_t                   lino = 0;
1,387✔
2952
  void*                     buf = NULL;
1,387✔
2953
  size_t                    size = 0;
1,387✔
2954
  SSDataBlock* pBlock = NULL;
1,387✔
2955

2956
  SMetaReader               metaReader = {0};
1,387✔
2957
  SMetaReader               metaReaderStable = {0};
1,387✔
2958
  int64_t streamId = req->base.streamId;
1,387✔
2959
  stsDebug("vgId:%d %s start", TD_VID(pVnode), __func__);
1,387✔
2960

2961
  SStorageAPI api = {0};
1,387✔
2962
  initStorageAPI(&api);
1,387✔
2963

2964
  SArray* cols = req->virTablePseudoColReq.cids;
1,387✔
2965
  STREAM_CHECK_NULL_GOTO(cols, terrno);
1,387!
2966

2967
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
1,387✔
2968
  STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, req->virTablePseudoColReq.uid));
1,387!
2969

2970
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
1,387!
2971

2972
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
1,387!
2973
  if (metaReader.me.type == TD_VIRTUAL_NORMAL_TABLE) {
1,387✔
2974
    STREAM_CHECK_CONDITION_GOTO (taosArrayGetSize(cols) < 1 || *(col_id_t*)taosArrayGet(cols, 0) != -1, TSDB_CODE_INVALID_PARA);
7!
2975
    SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
7✔
2976
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
7!
2977
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
7!
2978
    pBlock->info.rows = 1;
7✔
2979
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
7✔
2980
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
7!
2981
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
7!
2982
  } else if (metaReader.me.type == TD_VIRTUAL_CHILD_TABLE){
1,380!
2983
    int64_t suid = metaReader.me.ctbEntry.suid;
1,380✔
2984
    api.metaReaderFn.readerReleaseLock(&metaReader);
1,380✔
2985
    api.metaReaderFn.initReader(&metaReaderStable, pVnode, META_READER_LOCK, &api.metaFn);
1,380✔
2986

2987
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReaderStable, suid));
1,380!
2988
    SSchemaWrapper*  sSchemaWrapper = &metaReaderStable.me.stbEntry.schemaTag;
1,380✔
2989
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
3,644✔
2990
      col_id_t* id = taosArrayGet(cols, i);
2,264✔
2991
      STREAM_CHECK_NULL_GOTO(id, terrno);
2,264!
2992
      if (*id == -1) {
2,264✔
2993
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
1,373✔
2994
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
1,373!
2995
        continue;
1,373✔
2996
      }
2997
      size_t j = 0;
891✔
2998
      for (; j < sSchemaWrapper->nCols; j++) {
1,620!
2999
        SSchema* s = sSchemaWrapper->pSchema + j;
1,620✔
3000
        if (s->colId == *id) {
1,620✔
3001
          SColumnInfoData idata = createColumnInfoData(s->type, s->bytes, s->colId);
891✔
3002
          STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
891!
3003
          break;
891✔
3004
        }
3005
      }
3006
      if (j == sSchemaWrapper->nCols) {
891!
3007
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, *id);
×
3008
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
3009
      }
3010
    }
3011
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
1,380!
3012
    pBlock->info.rows = 1;
1,380✔
3013
    
3014
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
3,644✔
3015
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
2,264✔
3016
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
2,264!
3017

3018
      if (pDst->info.colId == -1) {
2,264✔
3019
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
1,373!
3020
        continue;
1,373✔
3021
      }
3022
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
891!
3023
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
3024
        continue;
×
3025
      }
3026

3027
      STagVal val = {0};
891✔
3028
      val.cid = pDst->info.colId;
891✔
3029
      const char* p = api.metaFn.extractTagVal(metaReader.me.ctbEntry.pTags, pDst->info.type, &val);
891✔
3030

3031
      char* data = NULL;
891✔
3032
      if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
891!
3033
        data = tTagValToData((const STagVal*)p, false);
891✔
3034
      } else {
3035
        data = (char*)p;
×
3036
      }
3037

3038
      STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data,
891!
3039
                            (data == NULL) || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data))));
3040

3041
      if ((pDst->info.type != TSDB_DATA_TYPE_JSON) && (p != NULL) && IS_VAR_DATA_TYPE(((const STagVal*)p)->type) &&
891!
3042
          (data != NULL)) {
3043
        taosMemoryFree(data);
714!
3044
      }
3045
    }
3046
  } else {
3047
    stError("vgId:%d %s, invalid table type:%d", TD_VID(pVnode), __func__, metaReader.me.type);
×
3048
    code = TSDB_CODE_INVALID_PARA;
×
3049
    goto end;
×
3050
  }
3051
  
3052
  stsDebug("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
1,387✔
3053
  printDataBlock(pBlock, __func__, "", streamId);
1,387✔
3054
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
1,387!
3055

3056
end:
1,387✔
3057
  if(size == 0){
1,387!
3058
    code = TSDB_CODE_STREAM_NO_DATA;
×
3059
  }
3060
  api.metaReaderFn.clearReader(&metaReaderStable);
1,387✔
3061
  api.metaReaderFn.clearReader(&metaReader);
1,387✔
3062
  STREAM_PRINT_LOG_END(code, lino);
1,387!
3063
  SRpcMsg rsp = {
1,387✔
3064
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3065
  tmsgSendRsp(&rsp);
1,387✔
3066
  blockDataDestroy(pBlock);
1,387✔
3067
  return code;
1,387✔
3068
}
3069

3070
static int32_t vnodeProcessStreamFetchMsg(SVnode* pVnode, SRpcMsg* pMsg) {
17,274✔
3071
  int32_t            code = 0;
17,274✔
3072
  int32_t            lino = 0;
17,274✔
3073
  void*              buf = NULL;
17,274✔
3074
  size_t             size = 0;
17,274✔
3075
  void*              taskAddr = NULL;
17,274✔
3076
  SArray*            pResList = NULL;
17,274✔
3077
  bool               hasNext = false;
17,274✔
3078

3079
  SResFetchReq req = {0};
17,274✔
3080
  STREAM_CHECK_CONDITION_GOTO(tDeserializeSResFetchReq(pMsg->pCont, pMsg->contLen, &req) < 0,
17,274!
3081
                              TSDB_CODE_QRY_INVALID_INPUT);
3082
  SArray* calcInfoList = (SArray*)qStreamGetReaderInfo(req.queryId, req.taskId, &taskAddr);
17,274✔
3083
  STREAM_CHECK_NULL_GOTO(calcInfoList, terrno);
17,274!
3084

3085
  STREAM_CHECK_CONDITION_GOTO(req.execId < 0, TSDB_CODE_INVALID_PARA);
17,274!
3086
  SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo = taosArrayGetP(calcInfoList, req.execId);
17,274✔
3087
  STREAM_CHECK_NULL_GOTO(sStreamReaderCalcInfo, terrno);
17,274!
3088
  void* pTask = sStreamReaderCalcInfo->pTask;
17,274✔
3089
  ST_TASK_DLOG("vgId:%d %s start, execId:%d, reset:%d, pTaskInfo:%p, scan type:%d", TD_VID(pVnode), __func__, req.execId, req.reset,
17,274✔
3090
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
3091

3092
  if (req.reset) {
17,274✔
3093
    int64_t uid = 0;
17,176✔
3094
    if (req.dynTbname) {
17,176✔
3095
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
140✔
3096
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
140!
3097
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
140✔
3098
        if (pValue != NULL && pValue->isTbname) {
140!
3099
          uid = pValue->uid;
140✔
3100
          break;
140✔
3101
        }
3102
      }
3103
    }
3104
    
3105
    SReadHandle handle = {0};
17,176✔
3106
    handle.vnode = pVnode;
17,176✔
3107
    handle.uid = uid;
17,176✔
3108

3109
    initStorageAPI(&handle.api);
17,176✔
3110
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
17,176✔
3111
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
15,965✔
3112
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
14,761✔
3113
      if (node != NULL) {
14,761✔
3114
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle));
987!
3115
      } else {
3116
        ST_TASK_DLOG("vgId:%d %s no time range node", TD_VID(pVnode), __func__);
13,776✔
3117
      }
3118
    }
3119

3120
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
17,176✔
3121
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
17,176✔
3122

3123
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
17,176✔
3124
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
792✔
3125
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
792✔
3126
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
3127
                                                    req.taskId));
3128
    } else {
3129
      STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle));
16,384!
3130
    }
3131

3132
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
17,173!
3133
  }
3134

3135
  if (req.pOpParam != NULL) {
17,272✔
3136
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, req.pOpParam);
818✔
3137
  }
3138
  
3139
  pResList = taosArrayInit(4, POINTER_BYTES);
17,272✔
3140
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
17,272!
3141
  uint64_t ts = 0;
17,272✔
3142
  STREAM_CHECK_RET_GOTO(qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL));
17,272✔
3143

3144
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
36,821✔
3145
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
19,559✔
3146
    if (pBlock == NULL) continue;
19,559!
3147
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
19,559✔
3148
/*    
3149
    if (sStreamReaderCalcInfo->rtInfo.funcInfo.withExternalWindow) {
3150
      STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderCalcInfo->pFilterInfo, NULL));
3151
      printDataBlock(pBlock, __func__, "fetch filter");
3152
    }
3153
*/    
3154
  }
3155

3156
end:
17,260✔
3157
  ST_TASK_DLOG("vgId:%d %s start to build rsp", TD_VID(pVnode), __func__);
17,272✔
3158
  STREAM_CHECK_RET_GOTO(streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision));
17,272!
3159
  ST_TASK_DLOG("vgId:%d %s end:", TD_VID(pVnode), __func__);
17,274✔
3160
  taosArrayDestroy(pResList);
17,274✔
3161
  streamReleaseTask(taskAddr);
17,274✔
3162

3163
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST){
17,274!
3164
    code = TDB_CODE_SUCCESS;
×
3165
  }
3166
  STREAM_PRINT_LOG_END(code, lino);
17,274!
3167
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
17,274✔
3168
  tmsgSendRsp(&rsp);
17,274✔
3169
  tDestroySResFetchReq(&req);
17,274✔
3170
  return code;
17,274✔
3171
}
3172

3173
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg) {
82,764✔
3174
  int32_t                   code = 0;
82,764✔
3175
  int32_t                   lino = 0;
82,764✔
3176
  SSTriggerPullRequestUnion req = {0};
82,764✔
3177
  void*                     taskAddr = NULL;
82,764✔
3178

3179
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
82,764✔
3180
  if (!syncIsReadyForRead(pVnode->sync)) {
82,765✔
3181
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
180✔
3182
    return 0;
181✔
3183
  }
3184

3185
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
82,588✔
3186
    return vnodeProcessStreamFetchMsg(pVnode, pMsg);
17,274✔
3187
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
65,314✔
3188
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
65,310✔
3189
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
65,310✔
3190
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
65,310!
3191
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64,
65,291✔
3192
            TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId);
3193
    SStreamTriggerReaderInfo* sStreamReaderInfo = (STRIGGER_PULL_OTABLE_INFO == req.base.type) ? NULL : qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
65,296✔
3194
    if (sStreamReaderInfo != NULL) {  
65,291✔
3195
      (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
65,001✔
3196
      if (sStreamReaderInfo->tableList == NULL) {
65,010✔
3197
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, false));  
610!
3198
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, true));
610!
3199
        STREAM_CHECK_RET_GOTO(filterInitFromNode(sStreamReaderInfo->pConditions, &sStreamReaderInfo->pFilterInfo, 0, NULL));
610!
3200
      }
3201
      (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
65,010✔
3202
      sStreamReaderInfo->pVnode = pVnode;
65,010✔
3203
    }
3204
    switch (req.base.type) {
65,300!
3205
      case STRIGGER_PULL_SET_TABLE:
240✔
3206
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo));
240!
3207
        break;
240✔
3208
      case STRIGGER_PULL_LAST_TS:
597✔
3209
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
597!
3210
        break;
597✔
3211
      case STRIGGER_PULL_FIRST_TS:
472✔
3212
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
472✔
3213
        break;
460✔
3214
      case STRIGGER_PULL_TSDB_META:
992✔
3215
      case STRIGGER_PULL_TSDB_META_NEXT:
3216
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo));
992✔
3217
        break;
980✔
3218
      case STRIGGER_PULL_TSDB_TS_DATA:
44✔
3219
        if (sStreamReaderInfo->isVtableStream) {
44✔
3220
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo));
1!
3221
        } else {
3222
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo));
43!
3223
        }
3224
        break;
44✔
3225
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
293✔
3226
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
3227
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
293!
3228
        break;
293✔
3229
      case STRIGGER_PULL_TSDB_CALC_DATA:
28,790✔
3230
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
3231
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
28,790✔
3232
        break;
28,435✔
3233
      case STRIGGER_PULL_TSDB_DATA:
823✔
3234
      case STRIGGER_PULL_TSDB_DATA_NEXT:
3235
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
823✔
3236
        break;
821✔
3237
      case STRIGGER_PULL_GROUP_COL_VALUE:
758✔
3238
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo));
758!
3239
        break;
757✔
3240
      case STRIGGER_PULL_VTABLE_INFO:
791✔
3241
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
791!
3242
        break;
791✔
3243
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
1,387✔
3244
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req));
1,387!
3245
        break;
1,387✔
3246
      case STRIGGER_PULL_OTABLE_INFO:
240✔
3247
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req));
240!
3248
        break;
240✔
3249
      case STRIGGER_PULL_WAL_META_NEW:
13,798✔
3250
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
13,798!
3251
        break;
13,799✔
3252
      case STRIGGER_PULL_WAL_DATA_NEW:
7,101✔
3253
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
7,101!
3254
        break;
7,097✔
3255
      case STRIGGER_PULL_WAL_META_DATA_NEW:
7,676✔
3256
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
7,676!
3257
        break;
7,660✔
3258
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
1,298✔
3259
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
1,298!
3260
        break;
1,298✔
3261
      default:
×
3262
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
×
3263
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3264
        break;
×
3265
    }
3266
  } else {
3267
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
4!
3268
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3269
  }
3270
end:
×
3271

3272
  streamReleaseTask(taskAddr);
65,276✔
3273

3274
  tDestroySTriggerPullRequest(&req);
65,307✔
3275
  STREAM_PRINT_LOG_END(code, lino);
65,298!
3276
  return code;
65,289✔
3277
}
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