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

taosdata / TDengine / #4774

01 Oct 2025 04:06AM UTC coverage: 58.357% (-0.3%) from 58.689%
#4774

push

travis-ci

web-flow
Merge pull request #33171 from taosdata/merge/3.3.6tomain

merge: from 3.3.6 to main branch

138553 of 302743 branches covered (45.77%)

Branch coverage included in aggregate %.

15 of 20 new or added lines in 2 files covered. (75.0%)

2558 existing lines in 138 files now uncovered.

209925 of 294403 relevant lines covered (71.31%)

5595496.87 hits per line

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

67.74
/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)); }
29,312✔
60

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

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

67
  if (*c1 < *c2) {
173✔
68
    return -1;
171✔
69
  } else if (*c1 > *c2) {
2!
70
    return 1;
2✔
71
  }
72

73
  return 0;
×
74
}
75

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

80
  if (c1->colId < c2->colId) {
173✔
81
    return -1;
171✔
82
  } else if (c1->colId > c2->colId) {
2!
83
    return 1;
2✔
84
  }
85

UNCOV
86
  return 0;
×
87
}
88

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

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

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

105
  return code;
33,740✔
106
}
107

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

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

130
static bool needRefreshTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
2,599✔
131
  if (sStreamReaderInfo->isVtableStream) {
2,599✔
132
    int64_t id[2] = {suid, uid};
830✔
133
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id)) == NULL) {
830!
134
      return true;
823✔
135
    }
136
  } else {
137
    if (tableType != TD_CHILD_TABLE) {
1,769✔
138
      return false;
677✔
139
    }
140
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
1,092✔
141
        suid == sStreamReaderInfo->suid && 
710✔
142
        qStreamGetGroupId(sStreamReaderInfo->tableList, uid) == -1) {
23✔
143
      return true;
9✔
144
    }
145
  }
146
  return false;
1,091✔
147
}
148

149
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc){
33,246✔
150
  if (sStreamReaderInfo->isVtableStream) {
33,246✔
151
    int64_t tmp[2] = {suid, uid};
8,569✔
152
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) == NULL) {
8,569✔
153
      return false;
2,832✔
154
    }
155
    *id = uid;
5,780✔
156
  } else {
157
    if (sStreamReaderInfo->tableList == NULL) return false;
24,677!
158

159
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
24,677✔
160
      if (suid != sStreamReaderInfo->suid) return false;
15,340✔
161
      if (sStreamReaderInfo->pTagCond == NULL) {
11,331✔
162
        if (sStreamReaderInfo->partitionCols == NULL){
9,769✔
163
          *id = 0;
52✔
164
        } else if (sStreamReaderInfo->groupByTbname){
9,717✔
165
          *id= uid;
9,014✔
166
        } else {
167
          *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
703✔
168
          if (*id == -1) return false;
704!
169
        }
170
      } else {
171
        //*id= uid;
172
        *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
1,562✔
173
        if (*id == -1) return false;
1,669✔
174
      }
175
    } else {
176
      *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
9,337✔
177
      if(*id == -1) *id = uid;
9,770✔
178
      return uid == sStreamReaderInfo->uid;
9,770✔
179
    }
180
  }
181
  return true;
16,521✔
182
}
183

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

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

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

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

238

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

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

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

275
end:
539✔
276
  STREAM_PRINT_LOG_END(code, lino);
539!
277
  return code;
539✔
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) {
6,145✔
301
  int32_t code = 0;
6,145✔
302
  int32_t lino = 0;
6,145✔
303
  int32_t index = 0;
6,145✔
304
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
6,145!
305
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
6,141!
306
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
6,126!
307
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
6,118!
308

309
end:
6,109✔
310
  return code;
6,109✔
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,
107✔
333
                              int64_t ver) {
334
  int32_t    code = 0;
107✔
335
  int32_t    lino = 0;
107✔
336
  SDecoder   decoder = {0};
107✔
337
  SDeleteRes req = {0};
107✔
338
  void* pTask = sStreamReaderInfo->pTask;
107✔
339

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

357
end:
67✔
358
  taosArrayDestroy(req.uidList);
106✔
359
  tDecoderClear(&decoder);
107✔
360
  return code;
107✔
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){
853✔
395
  (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
853✔
396
  qStreamDestroyTableList(sStreamReaderInfo->tableList);
853✔
397
  sStreamReaderInfo->tableList = NULL;
853✔
398
  int32_t code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, false);
853✔
399
  if (code == 0){
853!
400
    qStreamDestroyTableList(sStreamReaderInfo->historyTableList);
853✔
401
    sStreamReaderInfo->historyTableList = NULL;
853✔
402
    code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, true);
853✔
403
  }
404
  (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
853✔
405
  return code;
853✔
406
}
407

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

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

419
  bool found = false;
102✔
420
  SVCreateTbReq* pCreateReq = NULL;
102✔
421
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
155✔
422
    pCreateReq = req.pReqs + iReq;
102✔
423
    if (!needRefreshTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
102✔
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);
49!
428

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

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

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

451
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
783!
452
end:
783✔
453
  return code;
2,498✔
454
}
455

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

462
  SVAlterTbReq req = {0};
137✔
463
  tDecoderInit(&decoder, data, len);
137✔
464
  
465
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
137!
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);
136!
467

468
  ETableType tbType = 0;
35✔
469
  uint64_t suid = 0;
35✔
470
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, req.tbName, &tbType, &suid));
35!
471
  STREAM_CHECK_CONDITION_GOTO(tbType != TSDB_CHILD_TABLE, TDB_CODE_SUCCESS);
35!
472
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
35✔
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);
136✔
479
  tDecoderClear(&decoder);
136✔
480
  return code;
136✔
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) {
6,660✔
527
  int32_t code = 0;
6,660✔
528
  int32_t lino = 0;
6,660✔
529
  WalMetaResult walMeta = {0};
6,660✔
530
  SSubmitTbData submitTbData = {0};
6,660✔
531
  
532
  if (tStartDecode(pCoder) < 0) {
6,660!
533
    code = TSDB_CODE_INVALID_MSG;
×
534
    TSDB_CHECK_CODE(code, lino, end);
×
535
  }
536

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

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

553
  // submit data
554
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
6,647!
555
    code = TSDB_CODE_INVALID_MSG;
×
556
    TSDB_CHECK_CODE(code, lino, end);
×
557
  }
558
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
6,639!
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)){
6,639✔
564
    goto end;
4,432✔
565
  }
566
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
2,260!
567
    code = TSDB_CODE_INVALID_MSG;
×
568
    TSDB_CHECK_CODE(code, lino, end);
×
569
  }
570

571
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,260!
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;
2,260✔
601
    if (tDecodeU64v(pCoder, &nRow) < 0) {
2,255!
602
      code = TSDB_CODE_INVALID_MSG;
×
603
      TSDB_CHECK_CODE(code, lino, end);
×
604
    }
605

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

626
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
2,255✔
627
  if (data != NULL) {
2,250!
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)));
2,250!
632
  }
633

634
end:
2,249✔
635
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
6,681✔
636
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
6,649!
637
  tEndDecode(pCoder);
6,649✔
638
  return code;
6,650✔
639
}
640

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

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

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

660
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,665✔
661
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
6,663!
662

663
  for (int32_t i = 0; i < nSubmitTbData; i++) {
13,309✔
664
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash));
6,666!
665
  }
666
  tEndDecode(&decoder);
6,643✔
667

668
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
6,627!
669
  int32_t iter = 0;
6,624✔
670
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
6,624✔
671
  while (px != NULL) {
8,859✔
672
    WalMetaResult* pMeta = (WalMetaResult*)px;
2,232✔
673
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
2,232!
674
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
2,231✔
675
    rsp->totalRows++;
2,231✔
676
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
2,231✔
677
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
678
    px = tSimpleHashIterate(gidHash, px, &iter);
2,231✔
679
  }
680
end:
6,627✔
681
  tDecoderClear(&decoder);
6,627✔
682
  tSimpleHashCleanup( gidHash);
6,688✔
683
  return code;
6,672✔
684
}
685

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

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

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

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

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

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

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

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

724
end:
562✔
725
  taosArrayDestroy(schemas);
562✔
726
  return code;
562✔
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) {
889✔
749
  int32_t code = 0;
889✔
750
  int32_t lino = 0;
889✔
751
  SDecoder dcoder = {0};
889✔
752
  tDecoderInit(&dcoder, data, len);
889✔
753
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
889✔
754
    if (rsp->deleteBlock == NULL) {
107✔
755
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock));
39!
756
    }
757
      
758
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
107!
759
  } else if (msgType == TDMT_VND_DROP_TABLE && sStreamReaderInfo->deleteOutTbl != 0) {
782✔
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) {
781!
765
    STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
×
766
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
781✔
767
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, data, len));
102!
768
  } else if (msgType == TDMT_VND_ALTER_STB) {
679✔
769
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
770
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
500✔
771
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, data, len));
137!
772
  }
773

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

784
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
9,911✔
785
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
9,901!
786
  code = walReaderSeekVer(pWalReader, rsp->ver);
9,901✔
787
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
9,901✔
788
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
9,091!
789
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
790
    }
791
    ST_TASK_DLOG("vgId:%d %s scan wal error:%s", TD_VID(pVnode), __func__, tstrerror(code));
9,091✔
792
    code = TSDB_CODE_SUCCESS;
9,092✔
793
    goto end;
9,092✔
794
  }
795
  STREAM_CHECK_RET_GOTO(code);
810!
796

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

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

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

829
end:
9,902✔
830
  walCloseReader(pWalReader);
9,902✔
831
  return code;
9,916✔
832
}
833

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

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

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

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

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

875
    // this is to handle the tbname
876
    if (fmIsScanPseudoColumnFunc(functionId)) {
11,826✔
877
      int32_t fType = pExpr1->pExpr->_function.functionType;
7,082✔
878
      if (fType == FUNCTION_TYPE_TBNAME) {
7,082!
879
        char   buf[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
7,126✔
880
        if (uidData == NULL) {
7,126✔
881
          STR_TO_VARSTR(buf, mr.me.name)
344✔
882
          char* tbname = taosStrdup(mr.me.name);
344!
883
          STREAM_CHECK_NULL_GOTO(tbname, terrno);
344!
884
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &tbname), terrno);
688!
885
        } else {
886
          char* tbname = taosArrayGetP(tagCache, j);
6,782✔
887
          STR_TO_VARSTR(buf, tbname)
6,809✔
888
        }
889
        for (uint32_t i = 0; i < numOfRows; i++){
21,030✔
890
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
13,877✔
891
        }
892
        code = colDataSetNItems(pColInfoData, currentRow, buf, numOfRows, numOfBlocks, false);
7,153✔
893
        pColInfoData->info.colId = -1;
7,142✔
894
      }
895
    } else {  // these are tags
896
      char* data = NULL;
4,721✔
897
      const char* p = NULL;
4,721✔
898
      STagVal tagVal = {0};
4,721✔
899
      if (uidData == NULL) {
4,721✔
900
        tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
250✔
901
        p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, pColInfoData->info.type, &tagVal);
250✔
902

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

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

925
      bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
4,723!
926
      if (isNullVal) {
4,723!
927
        colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
928
      } else {
929
        for (uint32_t i = 0; i < numOfRows; i++){
35,894✔
930
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
31,171✔
931
        }
932
        code = colDataSetNItems(pColInfoData, currentRow, data, numOfRows, numOfBlocks, false);
4,723✔
933
        if (uidData == NULL && pColInfoData->info.type != TSDB_DATA_TYPE_JSON && IS_VAR_DATA_TYPE(((const STagVal*)p)->type)) {
4,738!
934
          taosMemoryFree(data);
72!
935
        }
936
        STREAM_CHECK_RET_GOTO(code);
4,737!
937
      }
938
    }
939
  }
940

941
end:
7,135✔
942
  api->metaReaderFn.clearReader(&mr);
7,135✔
943
  return code;
7,132✔
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, 
12,177✔
992
  STSchema** schemas, SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
993
  int32_t code = 0;
12,177✔
994
  int32_t lino = 0;
12,177✔
995
  uint64_t id = 0;
12,177✔
996
  WalMetaResult walMeta = {0};
12,177✔
997
  void* pTask = sStreamReaderInfo->pTask;
12,177✔
998
  SSDataBlock * pBlock = (SSDataBlock*)rsp->dataBlock;
12,177✔
999

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

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

1022
  // submit data
1023
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
12,104!
1024
    code = TSDB_CODE_INVALID_MSG;
×
1025
    TSDB_CHECK_CODE(code, lino, end);
×
1026
  }
1027
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
12,079!
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);
12,079✔
1033

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

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

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

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

1059
  int32_t numOfRows = 0;
9,155✔
1060
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
9,155!
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;
9,155✔
1148
    if (tDecodeU64v(pCoder, &nRow) < 0) {
9,152!
1149
      code = TSDB_CODE_INVALID_MSG;
×
1150
      TSDB_CHECK_CODE(code, lino, end);
×
1151
    }
1152
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
23,338✔
1153
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
14,242✔
1154
      pCoder->pos += pRow->len;
14,242✔
1155

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

1171
      if (pRow->ts < window.skey || pRow->ts > window.ekey) {
14,242✔
1172
        continue;
34✔
1173
      }
1174
     
1175
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {  // reader todo test null
101,391✔
1176
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
86,793✔
1177
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
86,897!
1178
        if (pColData->info.colId <= -1) {
87,263✔
1179
          pColData->hasNull = true;
27,411✔
1180
          continue;
27,411✔
1181
        }
1182
        int16_t colId = 0;
59,852✔
1183
        if (sStreamReaderInfo->isVtableStream){
59,852✔
1184
          int64_t id[2] = {submitTbData.suid, submitTbData.uid};
5,128✔
1185
          void* px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
5,128✔
1186
          STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
5,169!
1187
          SSHashObj* uInfo = *(SSHashObj**)px;
5,169✔
1188
          STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
5,169!
1189
          int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
5,169✔
1190
          if (tmp != NULL) {
5,130✔
1191
            colId = *tmp;
4,410✔
1192
          } else {
1193
            colId = -1;
720✔
1194
          }
1195
          ST_TASK_DLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
5,130✔
1196
        } else {
1197
          colId = pColData->info.colId;
54,724✔
1198
        }
1199
        
1200
        SColVal colVal = {0};
59,855✔
1201
        int32_t sourceIdx = 0;
59,855✔
1202
        while (1) {
1203
          if (sourceIdx >= (*schemas)->numOfCols) {
152,512✔
1204
            break;
30,332✔
1205
          }
1206
          STREAM_CHECK_RET_GOTO(tRowGet(pRow, *schemas, sourceIdx, &colVal));
122,180!
1207
          if (colVal.cid == colId) {
122,078✔
1208
            break;
29,421✔
1209
          }
1210
          sourceIdx++;
92,657✔
1211
        }
1212
        if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
59,753✔
1213
          if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL){
29,489!
1214
            STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart+ numOfRows, (const char*)colVal.value.pData, colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
173!
1215
          } else {
1216
            STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)), !COL_VAL_IS_VALUE(&colVal)));
29,316!
1217
          }
1218
        } else {
1219
          colDataSetNULL(pColData, blockStart + numOfRows);
30,264✔
1220
        }
1221
      }
1222
      
1223
      numOfRows++;
14,152✔
1224
    }
1225
  }
1226

1227
  if (numOfRows > 0) {
9,096✔
1228
    if (!sStreamReaderInfo->isVtableStream) {
9,093✔
1229
      SStorageAPI  api = {0};
6,906✔
1230
      initStorageAPI(&api);
6,906✔
1231
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, rsp->isCalc, &api, submitTbData.uid, pBlock, blockStart, numOfRows, 1));
6,918!
1232
    }
1233
    
1234
    SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
9,130✔
1235
    STREAM_CHECK_NULL_GOTO(pColData, terrno);
9,121!
1236
    STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
9,121!
1237
  }
1238

1239
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
9,156✔
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;
9,156✔
1243
  pBlock->info.rows += numOfRows;
9,156✔
1244
  
1245
  if (gidHash == NULL) goto end;
9,156✔
1246

1247
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
3,847✔
1248
  if (data != NULL) {
3,843!
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)));
3,843!
1253
  }
1254

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

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

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

1283
  if (rsp->metaBlock != NULL){
12,143✔
1284
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,833✔
1285
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
6,874!
1286
  }
1287

1288
  for (int32_t i = 0; i < nSubmitTbData; i++) {
24,264✔
1289
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, &schemas, ranges, gidHash, rsp, ver));
12,184!
1290
  }
1291

1292
  tEndDecode(&decoder);
12,080✔
1293

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

1309
end:
5,283✔
1310
  taosMemoryFree(schemas);
12,116!
1311
  tSimpleHashCleanup(gidHash);
12,175✔
1312
  tDecoderClear(&decoder);
12,146✔
1313
  return code;
12,178✔
1314
}
1315

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

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

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

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

1344
  // submit data
1345
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
14,558!
1346
    code = TSDB_CODE_INVALID_MSG;
×
1347
    TSDB_CHECK_CODE(code, lino, end);
×
1348
  }
1349
  if (tDecodeI64(pCoder, uid) < 0) {
14,489!
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);
14,489✔
1355

1356
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
8,976✔
1357

1358
  if (ranges != NULL){
8,976✔
1359
    void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
5,307✔
1360
    if (timerange == NULL) goto end;;
5,298!
1361
    int64_t* pRange = (int64_t*)timerange;
5,298✔
1362
    window.skey = pRange[0];
5,298✔
1363
    window.ekey = pRange[1];
5,298✔
1364
  }
1365
  
1366
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
9,077!
1367
    code = TSDB_CODE_INVALID_MSG;
×
1368
    TSDB_CHECK_CODE(code, lino, end);
×
1369
  }
1370

1371
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
9,077!
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;
9,077✔
1398
    if (tDecodeU64v(pCoder, &nRow) < 0) {
9,078!
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) { 
9,078!
1404
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
15,637✔
1405
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
10,337✔
1406
        pCoder->pos += pRow->len;
10,337✔
1407
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
10,337!
1408
          continue;
34✔
1409
        }
1410
        (*numOfRows)++;
10,303✔
1411
      }
1412
    } else {
1413
      (*numOfRows) = nRow;
3,778✔
1414
    }
1415
  }
1416
  
1417
end:
14,841✔
1418
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
14,841✔
1419
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
14,705!
1420
  tEndDecode(pCoder);
14,710✔
1421
  return code;
14,700✔
1422
}
1423

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

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

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

1442
  for (int32_t i = 0; i < nSubmitTbData; i++) {
29,467✔
1443
    uint64_t gid = -1;
14,639✔
1444
    int64_t  uid = 0;
14,639✔
1445
    int32_t numOfRows = 0;
14,639✔
1446
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp->isCalc));
14,639!
1447
    if (numOfRows <= 0) {
14,663✔
1448
      continue;
5,744✔
1449
    }
1450
    rsp->totalRows += numOfRows;
8,919✔
1451

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

1464
  tEndDecode(&decoder);
14,828✔
1465

1466
end:
14,598✔
1467
  tDecoderClear(&decoder);
14,598✔
1468
  return code;
14,781✔
1469
}
1470

1471
static void resetIndexHash(SSHashObj* indexHash){
12,602✔
1472
  void*   pe = NULL;
12,602✔
1473
  int32_t iter = 0;
12,602✔
1474
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
30,859✔
1475
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
18,257✔
1476
    pInfo->startRowIdx = 0;
18,257✔
1477
    pInfo->currentRowIdx = 0;
18,257✔
1478
    pInfo->numRows = 0;
18,257✔
1479
    pInfo->gId = -1;
18,257✔
1480
  }
1481
}
12,597✔
1482

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

1497
static void printIndexHash(SSHashObj* indexHash, void* pTask){
1,007✔
1498
  void*   pe = NULL;
1,007✔
1499
  int32_t iter = 0;
1,007✔
1500
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
3,560✔
1501
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
2,553✔
1502
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
3,280!
1503
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1504
  }
1505
}
1,009✔
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){
8,360✔
1530
  int32_t      code = 0;
8,360✔
1531
  int32_t      lino = 0;
8,360✔
1532
  void* pTask = sStreamReaderInfo->pTask;
8,360✔
1533

1534
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
8,360✔
1535
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
8,368✔
1536
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
7,310!
1537
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
1538
    }
1539
    ST_TASK_DLOG("%s scan wal error:%s",  __func__, tstrerror(code));
7,304✔
1540
    code = TSDB_CODE_SUCCESS;
7,303✔
1541
    goto end;
7,303✔
1542
  }
1543
  STREAM_CHECK_RET_GOTO(code);
1,058!
1544

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

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

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

1588
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
9,560✔
1589
    int64_t *ver = taosArrayGet(versions, i);
5,300✔
1590
    if (ver == NULL) continue;
5,304!
1591

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

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

1603
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp));
5,310!
1604
  }
1605
  
1606
end:
4,250✔
1607
  return code;
4,250✔
1608
}
1609

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

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

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

1641
  buildIndexHash(sStreamReaderInfo->indexHash, pTask);
348✔
1642
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
348!
1643
  while(lastVer < resultRsp->ver) {
7,505✔
1644
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
7,158!
1645
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
7,182✔
1646
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
295!
1647
      continue;
295✔
1648
    }
1649
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
6,887!
1650
    SWalCont* wCont = &pWalReader->pHead->head;
6,854✔
1651
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
6,854✔
1652
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
6,854✔
1653

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

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

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

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

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

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

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

1690
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
5,965✔
1691
    int64_t *ver = taosArrayGet(versions, i);
5,300✔
1692
    if (ver == NULL) continue;
5,296!
1693

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

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

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

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

1729
  SSchemaWrapper* sSchemaWrapper = NULL;
155✔
1730
  if (metaReader.me.type == TD_CHILD_TABLE) {
155!
1731
    int64_t suid = metaReader.me.ctbEntry.suid;
155✔
1732
    tDecoderClear(&metaReader.coder);
155✔
1733
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, suid));
155!
1734
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
155✔
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++) {
770✔
1742
    SSchema* s = sSchemaWrapper->pSchema + j;
615✔
1743
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
1,230!
1744
  }
1745

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

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

1775
end:
155✔
1776
  return code;
155✔
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,
994✔
1930
                                    STimeRangeNode* node, SReadHandle* handle) {
1931
  int32_t code = 0;
994✔
1932
  int32_t lino = 0;
994✔
1933
  SArray* funcVals = NULL;
994✔
1934
  if (req->pStRtFuncInfo->withExternalWindow) {
994✔
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;
291✔
1949
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
291✔
1950
    
1951
    SSTriggerCalcParam* pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
291✔
1952
    SSTriggerCalcParam* pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
291✔
1953
    STREAM_CHECK_NULL_GOTO(pFirst, terrno);
291!
1954
    STREAM_CHECK_NULL_GOTO(pLast, terrno);
291!
1955

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2117
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
406!
2118
  void* pTask = sStreamReaderInfo->pTask;
406✔
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);
406✔
2120
  BUILD_OPTION(options, sStreamReaderInfo, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, sStreamReaderInfo->tsSchemas, true,
406✔
2121
               STREAM_SCAN_GROUP_ONE_BY_ONE, req->firstTsReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2122
  SStorageAPI api = {0};
406✔
2123
  initStorageAPI(&api);
406✔
2124
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
406✔
2125
  
2126
  firstTsRsp.ver = pVnode->state.applied;
397✔
2127
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &firstTsRsp, sStreamReaderInfo, pTaskInner));
397!
2128

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

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

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

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

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

2162
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
752!
2163
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, sStreamReaderInfo->tsSchemas, true, 
752✔
2164
      (req->tsdbMetaReq.gid != 0 ? STREAM_SCAN_GROUP_ONE_BY_ONE : STREAM_SCAN_ALL), req->tsdbMetaReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2165
    SStorageAPI api = {0};
752✔
2166
    initStorageAPI(&api);
752✔
2167
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
752✔
2168
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
743!
2169
    
2170
    STREAM_CHECK_RET_GOTO(createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream));
743!
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);
743✔
2179
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
743!
2180
  bool hasNext = true;
743✔
2181
  while (true) {
209✔
2182
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
952!
2183
    if (!hasNext) {
953✔
2184
      break;
743✔
2185
    }
2186
    pTaskInner->api.tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
210✔
2187
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
210✔
2188

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

2198
    stDebug("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
209✔
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++;
209✔
2202
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
209!
2203
      break;
×
2204
    }
2205
  }
2206

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

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

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

2230
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
39!
2231
  void* pTask = sStreamReaderInfo->pTask;
39✔
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, 
39!
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,
39✔
2237
               sStreamReaderInfo->triggerCols, false, STREAM_SCAN_ALL, 0, true, NULL);
2238
  options.uid = req->tsdbTsDataReq.uid;
39✔
2239
  SStorageAPI api = {0};
39✔
2240
  initStorageAPI(&api);
39✔
2241
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
39!
2242
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
39!
2243
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
39!
2244

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

2255
    SSDataBlock* pBlock = NULL;
39✔
2256
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
39!
2257
    if (pBlock != NULL && pBlock->info.rows > 0) {
39!
2258
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, false, &api, pBlock->info.id.uid, pBlock,
39!
2259
          0, pBlock->info.rows, 1));
2260
    }
2261
    
2262
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
39!
2263
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
39!
2264
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
39!
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);
39✔
2270

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

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

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

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

UNCOV
2293
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
×
UNCOV
2294
  void* pTask = sStreamReaderInfo->pTask;
×
UNCOV
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, 
×
2296
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
2297
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
2298

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

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

UNCOV
2315
    SSDataBlock* pBlock = NULL;
×
UNCOV
2316
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
×
UNCOV
2317
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
×
UNCOV
2318
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
×
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

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

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

UNCOV
2333
  releaseStreamTask(&pTaskInner);
×
UNCOV
2334
  return code;
×
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)));
140!
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);
292✔
2368
  bool hasNext = true;
292✔
2369
  while (1) {
×
2370
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
292!
2371
    if (!hasNext) {
293✔
2372
      break;
141✔
2373
    }
2374
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
152✔
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);
292✔
2395
  if (!hasNext) {
292✔
2396
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
141!
2397
  }
2398

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

2405
  return code;
293✔
2406
}
2407

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

2415
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
28,267!
2416
  void* pTask = sStreamReaderInfo->pTask;
28,267✔
2417
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64, TD_VID(pVnode), __func__, 
28,267✔
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,267!
2421

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

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

2432
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
28,177!
2433
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
28,177!
2434
  } else {
2435
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
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,177✔
2442
  bool hasNext = true;
28,176✔
2443
  while (1) {
2,492✔
2444
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
30,668!
2445
    if (!hasNext) {
30,665✔
2446
      break;
28,172✔
2447
    }
2448
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
2,493✔
2449

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

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

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

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

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

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

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

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

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

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

2530
    STREAM_CHECK_RET_GOTO(qStreamInitQueryTableDataCond(&pTaskInner->cond, pTaskInner->options.order, pTaskInner->options.schemas,
155!
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,
155!
2534
                                                             (void**)&pTaskInner->pReader, pTaskInner->idStr, NULL));
2535
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pTaskInner->pResBlock, false, &pTaskInner->pResBlockDst));
155!
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);
155✔
2544
  bool hasNext = true;
155✔
2545
  while (1) {
155✔
2546
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
310!
2547
    if (!hasNext) {
310✔
2548
      break;
155✔
2549
    }
2550

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

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

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

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

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

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

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

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

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

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

2653
end:
8,381✔
2654
  if (resultRsp.totalRows == 0) {
8,381✔
2655
    buf = rpcMallocCont(sizeof(int64_t));
8,035✔
2656
    *(int64_t *)buf = resultRsp.ver;
8,042✔
2657
    size = sizeof(int64_t);
8,042✔
2658
    code = TSDB_CODE_STREAM_NO_DATA;
8,042✔
2659
  }
2660
  SRpcMsg rsp = {
8,388✔
2661
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2662
  tmsgSendRsp(&rsp);
8,388✔
2663
  if (code == TSDB_CODE_STREAM_NO_DATA){
8,394✔
2664
    code = 0;
8,045✔
2665
  }
2666
  blockDataDestroy(resultRsp.deleteBlock);
8,394✔
2667
  blockDataDestroy(resultRsp.dropBlock);
8,386✔
2668

2669
  STREAM_PRINT_LOG_END_WITHID(code, lino);
8,387!
2670

2671
  return code;
8,383✔
2672
}
2673

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

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

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

2689
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
3,567✔
2690

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

2697
end:
3,563✔
2698
  if (resultRsp.totalRows == 0) {
3,563✔
2699
    buf = rpcMallocCont(sizeof(int64_t));
3,473✔
2700
    *(int64_t *)buf = resultRsp.ver;
3,471✔
2701
    size = sizeof(int64_t);
3,471✔
2702
    code = TSDB_CODE_STREAM_NO_DATA;
3,471✔
2703
  }
2704
  SRpcMsg rsp = {
3,561✔
2705
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2706
  tmsgSendRsp(&rsp);
3,561✔
2707
  if (code == TSDB_CODE_STREAM_NO_DATA){
3,564✔
2708
    code = 0;
3,477✔
2709
  }
2710

2711
  blockDataDestroy(resultRsp.deleteBlock);
3,564✔
2712
  blockDataDestroy(resultRsp.dropBlock);
3,565✔
2713
  STREAM_PRINT_LOG_END_WITHID(code, lino);
3,567!
2714

2715
  return code;
3,566✔
2716
}
2717

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

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

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

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

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

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

2770
  return code;
690✔
2771
}
2772

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

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

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

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

2805
  return code;
741✔
2806
}
2807

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

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

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

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

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

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

2842
    code = api.metaReaderFn.getTableEntryByUid(&metaReader, pKeyInfo->uid);
570✔
2843
    if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
570!
2844
      vTable->cols.nCols = metaReader.me.colRef.nCols;
43✔
2845
      vTable->cols.version = metaReader.me.colRef.version;
43✔
2846
      vTable->cols.pColRef = taosMemoryCalloc(metaReader.me.colRef.nCols, sizeof(SColRef));
43!
2847
      for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
258✔
2848
        memcpy(vTable->cols.pColRef + j, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
215✔
2849
      }
2850
    } else {
2851
      vTable->cols.nCols = taosArrayGetSize(cids);
527✔
2852
      vTable->cols.version = metaReader.me.colRef.version;
527✔
2853
      vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
527!
2854
      for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
1,681✔
2855
        for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
4,375✔
2856
          if (metaReader.me.colRef.pColRef[j].hasRef &&
3,849✔
2857
              metaReader.me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
2,617✔
2858
            memcpy(vTable->cols.pColRef + i, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
629✔
2859
            break;
629✔
2860
          }
2861
        }
2862
      }
2863
    }
2864
    tDecoderClear(&metaReader.coder);
568✔
2865
  }
2866
  ST_TASK_DLOG("vgId:%d %s end", TD_VID(pVnode), __func__);
372✔
2867
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
372!
2868

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

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

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

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

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

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

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

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

2923
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
428!
2924
      SSchema* s = sSchemaWrapper->pSchema + j;
428✔
2925
      if (strcmp(s->name, oInfo->refColName) == 0) {
428✔
2926
        vTableInfo->cid = s->colId;
117✔
2927
        break;
117✔
2928
      }
2929
    }
2930
    if (vTableInfo->cid == 0) {
117!
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);
117✔
2935
  }
2936

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

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

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

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

2961
  SStorageAPI api = {0};
284✔
2962
  initStorageAPI(&api);
284✔
2963

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

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

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

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

2987
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReaderStable, suid));
276!
2988
    SSchemaWrapper*  sSchemaWrapper = &metaReaderStable.me.stbEntry.schemaTag;
276✔
2989
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
734✔
2990
      col_id_t* id = taosArrayGet(cols, i);
457✔
2991
      STREAM_CHECK_NULL_GOTO(id, terrno);
458!
2992
      if (*id == -1) {
458✔
2993
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
276✔
2994
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
276!
2995
        continue;
276✔
2996
      }
2997
      size_t j = 0;
182✔
2998
      for (; j < sSchemaWrapper->nCols; j++) {
182!
2999
        SSchema* s = sSchemaWrapper->pSchema + j;
182✔
3000
        if (s->colId == *id) {
182!
3001
          SColumnInfoData idata = createColumnInfoData(s->type, s->bytes, s->colId);
182✔
3002
          STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
182!
3003
          break;
182✔
3004
        }
3005
      }
3006
      if (j == sSchemaWrapper->nCols) {
182!
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));
276!
3012
    pBlock->info.rows = 1;
276✔
3013
    
3014
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
734✔
3015
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
456✔
3016
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
456!
3017

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

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

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

3038
      STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data,
182!
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) &&
182!
3042
          (data != NULL)) {
UNCOV
3043
        taosMemoryFree(data);
×
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);
285✔
3053
  printDataBlock(pBlock, __func__, "", streamId);
285✔
3054
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
283!
3055

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

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

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

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

3091
  if (req.reset || sStreamReaderCalcInfo->pTaskInfo == NULL) {
15,309!
3092
  // if (req.reset) {
3093
    qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
15,220✔
3094
    int64_t uid = 0;
15,218✔
3095
    if (req.dynTbname) {
15,218✔
3096
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
699✔
3097
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
699!
3098
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
699✔
3099
        if (pValue != NULL && pValue->isTbname) {
699!
3100
          uid = pValue->uid;
699✔
3101
          break;
699✔
3102
        }
3103
      }
3104
    }
3105
    
3106
    SReadHandle handle = {0};
15,218✔
3107
    handle.vnode = pVnode;
15,218✔
3108
    handle.uid = uid;
15,218✔
3109

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

3121
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
15,219✔
3122
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
15,219✔
3123

3124
    // if (sStreamReaderCalcInfo->pTaskInfo == NULL) {
3125
    STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
15,219✔
3126
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
3127
                                                    req.taskId));
3128
    // } else {
3129
    // STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, handle.winRange));
3130
    // }
3131

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

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

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

3157
  ST_TASK_DLOG("vgId:%d %s start to build rsp", TD_VID(pVnode), __func__);
15,287✔
3158
  STREAM_CHECK_RET_GOTO(streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision));
15,287!
3159
  ST_TASK_DLOG("vgId:%d %s end:", TD_VID(pVnode), __func__);
15,287✔
3160

3161
end:
216✔
3162
  taosArrayDestroy(pResList);
15,309✔
3163
  streamReleaseTask(taskAddr);
15,309✔
3164

3165
  STREAM_PRINT_LOG_END(code, lino);
15,309!
3166
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
15,309✔
3167
  tmsgSendRsp(&rsp);
15,309✔
3168
  tDestroySResFetchReq(&req);
15,309✔
3169
  return code;
15,309✔
3170
}
3171

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

3178
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
69,948✔
3179
  if (!syncIsReadyForRead(pVnode->sync)) {
69,949✔
3180
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
96✔
3181
    return 0;
98✔
3182
  }
3183

3184
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
69,856✔
3185
    return vnodeProcessStreamFetchMsg(pVnode, pMsg);
15,309✔
3186
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
54,547✔
3187
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
54,545✔
3188
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
54,545✔
3189
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
54,545!
3190
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64,
54,536✔
3191
            TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId);
3192
    SStreamTriggerReaderInfo* sStreamReaderInfo = (STRIGGER_PULL_OTABLE_INFO == req.base.type) ? NULL : qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
54,542✔
3193
    if (sStreamReaderInfo != NULL) {  
54,534✔
3194
      (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
54,437✔
3195
      if (sStreamReaderInfo->tableList == NULL) {
54,444✔
3196
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, false));  
524!
3197
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, true));
524!
3198
        STREAM_CHECK_RET_GOTO(filterInitFromNode(sStreamReaderInfo->pConditions, &sStreamReaderInfo->pFilterInfo, 0, NULL));
524!
3199
      }
3200
      (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
54,444✔
3201
      sStreamReaderInfo->pVnode = pVnode;
54,442✔
3202
    }
3203
    switch (req.base.type) {
54,539!
3204
      case STRIGGER_PULL_SET_TABLE:
59✔
3205
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo));
59!
3206
        break;
59✔
3207
      case STRIGGER_PULL_LAST_TS:
505✔
3208
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
505!
3209
        break;
505✔
3210
      case STRIGGER_PULL_FIRST_TS:
406✔
3211
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
406✔
3212
        break;
397✔
3213
      case STRIGGER_PULL_TSDB_META:
752✔
3214
      case STRIGGER_PULL_TSDB_META_NEXT:
3215
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo));
752✔
3216
        break;
743✔
3217
      case STRIGGER_PULL_TSDB_TS_DATA:
39✔
3218
        if (sStreamReaderInfo->isVtableStream) {
39!
UNCOV
3219
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo));
×
3220
        } else {
3221
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo));
39!
3222
        }
3223
        break;
39✔
3224
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
293✔
3225
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
3226
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
293!
3227
        break;
293✔
3228
      case STRIGGER_PULL_TSDB_CALC_DATA:
28,267✔
3229
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
3230
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
28,267✔
3231
        break;
28,174✔
3232
      case STRIGGER_PULL_TSDB_DATA:
155✔
3233
      case STRIGGER_PULL_TSDB_DATA_NEXT:
3234
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
155!
3235
        break;
155✔
3236
      case STRIGGER_PULL_GROUP_COL_VALUE:
741✔
3237
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo));
741!
3238
        break;
741✔
3239
      case STRIGGER_PULL_VTABLE_INFO:
372✔
3240
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
372!
3241
        break;
372✔
3242
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
284✔
3243
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req));
284!
3244
        break;
284✔
3245
      case STRIGGER_PULL_OTABLE_INFO:
59✔
3246
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req));
59!
3247
        break;
59✔
3248
      case STRIGGER_PULL_WAL_META_NEW:
9,961✔
3249
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
9,961!
3250
        break;
9,956✔
3251
      case STRIGGER_PULL_WAL_DATA_NEW:
3,568✔
3252
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
3,568!
3253
        break;
3,563✔
3254
      case STRIGGER_PULL_WAL_META_DATA_NEW:
8,387✔
3255
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
8,387!
3256
        break;
8,382✔
3257
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
691✔
3258
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
691!
3259
        break;
690✔
3260
      default:
×
3261
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
×
3262
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3263
        break;
×
3264
    }
3265
  } else {
3266
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
2!
3267
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3268
  }
3269
end:
×
3270

3271
  streamReleaseTask(taskAddr);
54,520✔
3272

3273
  tDestroySTriggerPullRequest(&req);
54,544✔
3274
  STREAM_PRINT_LOG_END(code, lino);
54,531!
3275
  return code;
54,521✔
3276
}
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